From b009b2a6e4ae67c8109b0c5435172a7bdd4805ba Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Wed, 24 Mar 2021 19:55:45 +0200 Subject: [PATCH] CAKE-272 | refactored script --- tool/generate_localization.dart | 159 ++++++++++++++++---------------- 1 file changed, 81 insertions(+), 78 deletions(-) diff --git a/tool/generate_localization.dart b/tool/generate_localization.dart index e95329571..ee9196bb3 100644 --- a/tool/generate_localization.dart +++ b/tool/generate_localization.dart @@ -28,85 +28,88 @@ Future main(List args) async { : {srcDir : inputPath}; extraInfo.forEach((key, dynamic value) async { - if (key == srcDir) { - final dirPath = value as String; - final dir = Directory(dirPath); - - if (await dir.exists()) { - final localePath = {}; - await dir.list(recursive: false).forEach((element) { - try { - final shortLocale = element.path.split('_',)[1].split('.')[0]; - localePath[shortLocale] = element.path; - } catch (e) { - print('Wrong file: ${element.path}'); - } - }); - - if (localePath.keys.contains(defaultLocale)) { - try { - var output = ''; - var locales = 'const locales = ['; - - output += part1; - output += textDirectionDeclaration; - - var inputContent = - File(localePath[defaultLocale].toString()).readAsStringSync(); - var config = json.decode(inputContent) as Map; - - output += localizedStrings(config: config, hasOverride: false); - output += '}' + '\n\n'; - - localePath.forEach((key, dynamic value) { - inputContent = File(localePath[key].toString()).readAsStringSync(); - config = json.decode(inputContent) as Map; - - locales += "'$key', "; - - output += 'class \$$key extends S {' + '\n'; - output += ' const \$$key();' + '\n'; - - if (key != defaultLocale) { - output += textDirectionDeclaration; - output += localizedStrings(config: config, hasOverride: true); - } - - output += '}' + '\n\n'; - }); - - output += classDeclaration; - - localePath.keys.forEach((key) { - output += ' Locale("$key", ""),' + '\n'; - }); - - output += part2; - - localePath.keys.forEach((key) { - output += ' case "$key":' + '\n'; - output += ' S.current = const \$$key();' + '\n'; - output += ' return SynchronousFuture(S.current);' + '\n'; - }); - - output += part3; - - await File(outputPath + localizationFileName).writeAsString(output); - - locales += '];'; - - await File(outputPath + localeListFileName).writeAsString(locales); - } catch (e) { - print(e.toString()); - } - } else { - print("Locale list doesn't contain $defaultLocale"); - } - } else { - print('Wrong directory path: $dirPath'); - } - } else { + if (key != srcDir) { print('Wrong key: $key'); + return; + } + + final dirPath = value as String; + final dir = Directory(dirPath); + + if (!await dir.exists()) { + print('Wrong directory path: $dirPath'); + return; + } + + final localePath = {}; + await dir.list(recursive: false).forEach((element) { + try { + final shortLocale = element.path.split('_',)[1].split('.')[0]; + localePath[shortLocale] = element.path; + } catch (e) { + print('Wrong file: ${element.path}'); + } + }); + + if (!localePath.keys.contains(defaultLocale)) { + print("Locale list doesn't contain $defaultLocale"); + return; + } + + try { + var output = ''; + var locales = 'const locales = ['; + + output += part1; + output += textDirectionDeclaration; + + var inputContent = + File(localePath[defaultLocale].toString()).readAsStringSync(); + var config = json.decode(inputContent) as Map; + + output += localizedStrings(config: config, hasOverride: false); + output += '}' + '\n\n'; + + localePath.forEach((key, dynamic value) { + inputContent = File(localePath[key].toString()).readAsStringSync(); + config = json.decode(inputContent) as Map; + + locales += "'$key', "; + + output += 'class \$$key extends S {' + '\n'; + output += ' const \$$key();' + '\n'; + + if (key != defaultLocale) { + output += textDirectionDeclaration; + output += localizedStrings(config: config, hasOverride: true); + } + + output += '}' + '\n\n'; + }); + + output += classDeclaration; + + localePath.keys.forEach((key) { + output += ' Locale("$key", ""),' + '\n'; + }); + + output += part2; + + localePath.keys.forEach((key) { + output += ' case "$key":' + '\n'; + output += ' S.current = const \$$key();' + '\n'; + output += ' return SynchronousFuture(S.current);' + '\n'; + }); + + output += part3; + + await File(outputPath + localizationFileName).writeAsString(output); + + locales += '];'; + + await File(outputPath + localeListFileName).writeAsString(locales); + } catch (e) { + print(e.toString()); } }); }