Skip to content

Commit

Permalink
Merge pull request #13 from VictorKachalov/hotfix-2.0.3
Browse files Browse the repository at this point in the history
Hotfix 2.0.3
  • Loading branch information
kvs-coder authored Nov 18, 2020
2 parents ef59a7a + 102ad68 commit c49bb60
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ flutter:
- res/localizations/
```

The algorithm will check if you have already created the localization files
and will ask you if you want to overwrite them. If you select **Y** in the
command line, the files and the content will be overwritten. In case of **N**
nothing will change. If you enter other character the function will call itself
recursively until you provide a valid input.

Next is to do some coding

- Import the library
Expand Down Expand Up @@ -259,6 +265,11 @@ flutter pub run localized:main -t -l ru,en,de -p Microsoft -m YOUR_MICROSOFT_KEY

(see list of [Parameters](#parameters))

The algorithm checks the amount of localization files you have and requires at least two of them.
It compares the amount of key-value pairs in several JSON files and lets translation
running for those JSON files, which don't have the actual translated key-value pairs. It will not
overwrite the already existing translation.

Please pay attention to MAXIMUM_BUFFER_NUMBER parameter. This parameter shows how many strings are allowed to be translated in one request.
By default, the parameter value is set to 1 and generally it will take much longer time for the translation.

Expand Down
40 changes: 26 additions & 14 deletions bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final Map<String, String> _providerDescriptionMap = {
};

/// Main entry point for every [flutter pub run localized:main] script
///
/// Creates examples of localized files as well as localized strings
/// and translates them using different providers.
///
Expand All @@ -42,8 +43,13 @@ final Map<String, String> _providerDescriptionMap = {
/// Do not use both parameters in one call.
///
/// First step:
/// JSON files creation
/// Example of creation script:
/// - JSON files creation
/// The algorithm will check if you have already created the localization files
/// and will ask you if you want to overwrite them. If you select "Y" in the
/// command line, the files and the content will be overwritten. In case of "N"
/// nothing will change. If you enter other character the function will call itself
/// recursively until you provide a valid input.
/// - Example of creation script:
/// {
/// "default_dir": {
/// "script": "flutter pub run localized:main -c -l en,de,ru"
Expand All @@ -54,8 +60,12 @@ final Map<String, String> _providerDescriptionMap = {
/// }
///
/// Second step:
/// Existing strings in existing JSON file translation
/// Examples of translation scripts:
/// - Existing strings in existing JSON file translation
/// The algorithm is based on the amount of localization files you have and requires at least two of them.
/// The logic is that it compares the amount of key-value pairs in several JSON files and lets translation
/// running for those JSON files, which don't have the actual translated key-value pairs. It will not
/// overwrite the already existing translation.
/// - Examples of translation scripts:
/// {
/// "google": {
/// "script": "flutter pub run localized:main -t -l ru,en,de -p Google -k YOUR_GOOGLE_KEY -n 25"
Expand All @@ -70,7 +80,7 @@ final Map<String, String> _providerDescriptionMap = {
///
/// See [README.md] and usage for details
///
Future<void> main(List<String> args) async {
void main(List<String> args) async {
final parser = ArgParser(allowTrailingOptions: true);
parser.addFlag('create',
abbr: 'c',
Expand Down Expand Up @@ -209,7 +219,7 @@ Future<void> _translateLocalizedFiles(
stdout.writeln('Language code $lang is not supported.');
continue;
}
langStringMap[lang] = _loadStrings(lang, dirPath);
langStringMap[lang] = await _loadStrings(lang, dirPath);
}

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

/// see https://github.com/gabrielpacheco23, thanks to Gabriel Pacheco
///
Future<void> _translateGoogleTest(Map<String, Map<String, String>> langStringMap,
Future<void> _translateGoogleTest(
Map<String, Map<String, String>> langStringMap,
Map<Tuple2<String, String>, List<String>> toTranslateMap) async {
final gtr = GoogleTranslator();
await Future.forEach(toTranslateMap.entries, (toTranslate) async {
Expand Down Expand Up @@ -396,8 +408,8 @@ Future<void> _translateYandex(List<String> stringInOutList, String sourceLang,
/// [langStringMap] is a map of a language to <key, string>
/// [toTranslateMap] is a map of <targetLang, sourceLang> to List<key>> - strings to translate
///
Future<void> _translateMicrosoft(List<String> stringInOutList, String sourceLang,
String targetLang, Map<String, String> options) async {
Future<void> _translateMicrosoft(List<String> stringInOutList,
String sourceLang, String targetLang, Map<String, String> options) async {
final msEndpoint = options['endpoint'];
final msKey = options['ms_key'];
final msRegion = options['region'];
Expand Down Expand Up @@ -436,14 +448,14 @@ Future<void> _translateMicrosoft(List<String> stringInOutList, String sourceLang

/// Loading of strings from language files
///
Map<String, String> _loadStrings(String lang, String dirPath) {
Future<Map<String, String>> _loadStrings(String lang, String dirPath) async {
var localizedStrings = <String, String>{};
try {
final file = File('$dirPath/$lang.json');
if (!file.existsSync()) {
if (!await file.exists()) {
return localizedStrings;
}
final jsonString = file.readAsStringSync();
final jsonString = await file.readAsString();
if (jsonString.isEmpty) {
return localizedStrings;
}
Expand Down
2 changes: 1 addition & 1 deletion example/assets/i18n/de.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"title":"Startseite","amount_of_clicks":"Mal hast du den Knopf gedrückt:","increase":"Erhöhen, ansteigen"}
{"title":"Startseite","amount_of_clicks":"Anzahl der Klicks","increase":"Erhöhen"}
2 changes: 1 addition & 1 deletion example/assets/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"title":"Home page","amount_of_clicks":"Erhöhen, ansteigen","increase":"Erhöhen, ansteigen"}
{"title":"Home page","amount_of_clicks":"Amount of clicks","increase":"Increase"}
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":"Times you pressed the button:","increase":"Erhöhen, ansteigen"}
{"title":"Главная страница","amount_of_clicks":"Количество кликов","increase":"Увеличить"}

0 comments on commit c49bb60

Please sign in to comment.