Skip to content

Commit

Permalink
2.0.2 preps
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Kachalov committed Nov 18, 2020
1 parent aae57d6 commit 11bc608
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.0.2] - 18.11.2020

* Code refactoring

## [2.0.1] - 18.11.2020

* Minor fixes
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ String extensions for localization and a translation utility
- Add this to your package's pubspec.yaml file:
``` Dart
dependencies:
localized: ^2.0.1
localized: ^2.0.2
```
- Get dependencies

Expand Down
45 changes: 24 additions & 21 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void main(List<String> args) async {
/// if there are empty keys.
///
void _translateLocalizedFiles(
List<String> langCodes, String dirPath, Map<String, String> options) async {
List<String> langCodes, String dirPath, Map<String, String> options) {
final provider = options['provider'];
if (provider == null || !_providerList.contains(provider)) {
stdout.writeln('No valid translation provider set. Exiting...');
Expand Down Expand Up @@ -172,7 +172,7 @@ void _translateLocalizedFiles(
stdout.writeln('Language code $lang is not supported.');
continue;
}
langStringMap[lang] = await _loadStrings(lang, dirPath);
langStringMap[lang] = _loadStrings(lang, dirPath);
}

/// looking for and collecting strings that exist for one language, but don't for another
Expand Down Expand Up @@ -206,10 +206,9 @@ void _translateLocalizedFiles(
/// different functions and arguments for different providers
///
provider.compareTo(_providerList[0]) == 0
? await _providerTranslateFunctionMap[provider](
langStringMap, toTranslateMap)
: await _batchTranslate(langStringMap, toTranslateMap, options);
await _updateContent(langStringMap, dirPath);
? _providerTranslateFunctionMap[provider](langStringMap, toTranslateMap)
: _batchTranslate(langStringMap, toTranslateMap, options);
_updateContent(langStringMap, dirPath);
}

/// see https://github.com/gabrielpacheco23, thanks to Gabriel Pacheco
Expand All @@ -223,9 +222,9 @@ void _translateGoogleTest(Map<String, Map<String, String>> langStringMap,
final keyList = toTranslate.value;
await Future.forEach(keyList, (key) async {
final sourceString = langStringMap[sourceLang][key];
langStringMap[targetLang][key] =
(await gtr.translate(sourceString, from: sourceLang, to: targetLang))
.text;
final translated =
await gtr.translate(sourceString, from: sourceLang, to: targetLang);
langStringMap[targetLang][key] = translated.text;
});
});
}
Expand Down Expand Up @@ -253,7 +252,7 @@ void _batchTranslate(
stringInOutList.add(langStringMap[sourceLang][key]);
++num;
if (num > 0 && num % numStringsAtOnce == 0) {
await _providerTranslateFunctionMap[provider](
_providerTranslateFunctionMap[provider](
stringInOutList, sourceLang, targetLang, options);
for (var index = 0; index < stringInOutList.length; index++) {
langStringMap[targetLang]
Expand All @@ -264,7 +263,7 @@ void _batchTranslate(
}
}
if (stringInOutList.isNotEmpty) {
await _providerTranslateFunctionMap[provider](
_providerTranslateFunctionMap[provider](
stringInOutList, sourceLang, targetLang, options);
for (var index = 0; index < stringInOutList.length; index++) {
langStringMap[targetLang]
Expand Down Expand Up @@ -400,13 +399,17 @@ void _translateMicrosoft(List<String> stringInOutList, String sourceLang,

/// Loading of strings from language files
///
Future<Map<String, String>> _loadStrings(String lang, String dirPath) async {
Map<String, String> _loadStrings(String lang, String dirPath) {
var localizedStrings = <String, String>{};
try {
final file = File('$dirPath/$lang.json');
if (!await file.exists()) return localizedStrings;
final jsonString = await file.readAsString();
if (jsonString.isEmpty) return localizedStrings;
if (!file.existsSync()) {
return localizedStrings;
}
final jsonString = file.readAsStringSync();
if (jsonString.isEmpty) {
return localizedStrings;
}
final Map<String, dynamic> jsonMap = json.decode(jsonString);
localizedStrings = jsonMap.map((key, value) {
return MapEntry(key, value.toString());
Expand All @@ -424,12 +427,12 @@ void _updateContent(
Map<String, Map<String, String>> langStrMap, dirPath) async {
await Future.forEach(langStrMap.entries, (langStrMapEntry) async {
try {
await File('$dirPath/${langStrMapEntry.key}.json')
.create(recursive: true)
.then((file) {
stdout.writeln('Rewriting file: ${file.path}');
file.writeAsString(json.encode(langStrMapEntry.value));
});
final fileName = '$dirPath/${langStrMapEntry.key}.json';
final file = await File(fileName).create(recursive: true);
stdout.writeln('Rewriting file: ${file.path}');
final writtenFiled =
await file.writeAsString(json.encode(langStrMapEntry.value));
stdout.writeln('File rewriting finished: ${writtenFiled.path}');
} catch (e) {
stdout.writeln(
'Cannot update $dirPath/${langStrMapEntry.key}.json file. An exception occurs:\n$e.');
Expand Down
2 changes: 1 addition & 1 deletion example/assets/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"title":"Главная страница","amount_of_clicks":"Времена, когда вы нажали на кнопку:","increase":"Увеличить"}
{"title":"Главная страница","amount_of_clicks":"Times you pressed the button:","increase":"Increase"}
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.0"
version: "1.9.2"
platform:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: localized
description: String extensions for different localizations and a translation utility without additional efforts.
version: 2.0.1
version: 2.0.2
homepage: https://github.com/VictorKachalov/localized

environment:
Expand All @@ -17,7 +17,7 @@ dependencies:
translator: ^0.1.5

dev_dependencies:
pedantic: ^1.9.0
pedantic: ^1.9.2
flutter_test:
sdk: flutter

Expand Down

0 comments on commit 11bc608

Please sign in to comment.