From 2eed59c1d5e0bd0ce72b28116e30d0a1092dc729 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Fri, 13 Dec 2019 10:03:56 -0800 Subject: [PATCH] Add gen_l10n sorting tests (#46247) * Add tests for sorting arbPathString, supportedLocales and supportedLanguageCodes alphabetically * Plural arbPathStrings * Factor out singleZhMessageArbFileString * Make translations more accurate and generic --- dev/tools/localization/bin/gen_l10n.dart | 2 +- dev/tools/localization/gen_l10n.dart | 7 +- .../test/localization/gen_l10n_test.dart | 89 ++++++++++++++++--- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/dev/tools/localization/bin/gen_l10n.dart b/dev/tools/localization/bin/gen_l10n.dart index 642c5d309a..06a46db6c1 100644 --- a/dev/tools/localization/bin/gen_l10n.dart +++ b/dev/tools/localization/bin/gen_l10n.dart @@ -92,7 +92,7 @@ Future main(List arguments) async { '--output-dir=${localizationsGenerator.l10nDirectory.path}', '--no-use-deferred-loading', localizationsGenerator.outputFile.path, - ...localizationsGenerator.arbFilenames, + ...localizationsGenerator.arbPathStrings, ]); if (generateFromArbResult.exitCode != 0) { stderr.write(generateFromArbResult.stderr); diff --git a/dev/tools/localization/gen_l10n.dart b/dev/tools/localization/gen_l10n.dart index 2cc57f8b8f..d5202e9c71 100644 --- a/dev/tools/localization/gen_l10n.dart +++ b/dev/tools/localization/gen_l10n.dart @@ -359,8 +359,8 @@ class LocalizationsGenerator { /// Sets the [className] for the localizations and localizations delegate /// classes. - /// The list of all arb files in [l10nDirectory]. - final List arbFilenames = []; + /// The list of all arb path strings in [l10nDirectory]. + final List arbPathStrings = []; /// The supported language codes as found in the arb files located in /// [l10nDirectory]. @@ -477,7 +477,7 @@ class LocalizationsGenerator { localeString = arbFilenameLocaleRE.firstMatch(filePath)[1]; } - arbFilenames.add(filePath); + arbPathStrings.add(filePath); final LocaleInfo localeInfo = LocaleInfo.fromString(localeString); if (localeInfoList.contains(localeInfo)) throw L10nException( @@ -488,6 +488,7 @@ class LocalizationsGenerator { } } + arbPathStrings.sort(); localeInfoList.sort(); supportedLocales.addAll(localeInfoList); supportedLanguageCodes.addAll(localeInfoList.map((LocaleInfo localeInfo) { diff --git a/dev/tools/test/localization/gen_l10n_test.dart b/dev/tools/test/localization/gen_l10n_test.dart index 872bec7617..2aca69d1c6 100644 --- a/dev/tools/test/localization/gen_l10n_test.dart +++ b/dev/tools/test/localization/gen_l10n_test.dart @@ -18,15 +18,18 @@ const String defaultTemplateArbFileName = 'app_en_US.arb'; const String defaultOutputFileString = 'output-localization-file'; const String defaultClassNameString = 'AppLocalizations'; const String singleMessageArbFileString = '''{ - "title": "Stocks", + "title": "Title", "@title": { - "description": "Title for the Stocks application" + "description": "Title for the application" } }'''; const String esArbFileName = 'app_es.arb'; const String singleEsMessageArbFileString = '''{ - "title": "Acciones" + "title": "Título" +}'''; +const String singleZhMessageArbFileString = '''{ + "title": "标题" }'''; void _standardFlutterDirectoryL10nSetup(FileSystem fs) { @@ -261,20 +264,86 @@ void main() { expect(generator.supportedLocales.contains(LocaleInfo.fromString('es')), true); }); + test('correctly sorts supportedLocales and supportedLanguageCodes alphabetically', () { + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + // Write files in non-alphabetical order so that read performs in that order + l10nDirectory.childFile('app_zh.arb') + .writeAsStringSync(singleZhMessageArbFileString); + l10nDirectory.childFile('app_es.arb') + .writeAsStringSync(singleEsMessageArbFileString); + l10nDirectory.childFile('app_en_US.arb') + .writeAsStringSync(singleMessageArbFileString); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + ); + generator.parseArbFiles(); + } on L10nException catch (e) { + fail('Setting language and locales should not fail: \n$e'); + } + + expect(generator.supportedLocales.first, LocaleInfo.fromString('en_US')); + expect(generator.supportedLocales.elementAt(1), LocaleInfo.fromString('es')); + expect(generator.supportedLocales.elementAt(2), LocaleInfo.fromString('zh')); + }); + + test('correctly sorts arbPathString alphabetically', () { + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + // Write files in non-alphabetical order so that read performs in that order + l10nDirectory.childFile('app_zh.arb') + .writeAsStringSync(singleZhMessageArbFileString); + l10nDirectory.childFile('app_es.arb') + .writeAsStringSync(singleEsMessageArbFileString); + l10nDirectory.childFile('app_en_US.arb') + .writeAsStringSync(singleMessageArbFileString); + + LocalizationsGenerator generator; + try { + generator = LocalizationsGenerator(fs); + generator.initialize( + l10nDirectoryPath: defaultArbPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + ); + generator.parseArbFiles(); + } on L10nException catch (e) { + fail('Setting language and locales should not fail: \n$e'); + } + + if (Platform.isWindows) { + expect(generator.arbPathStrings.first, 'lib\\l10n\\app_en_US.arb'); + expect(generator.arbPathStrings.elementAt(1), 'lib\\l10n\\app_es.arb'); + expect(generator.arbPathStrings.elementAt(2), 'lib\\l10n\\app_zh.arb'); + } else { + expect(generator.arbPathStrings.first, 'lib/l10n/app_en_US.arb'); + expect(generator.arbPathStrings.elementAt(1), 'lib/l10n/app_es.arb'); + expect(generator.arbPathStrings.elementAt(2), 'lib/l10n/app_zh.arb'); + } + }); + test('correctly parses @@locale property in arb file', () { const String arbFileWithEnLocale = '''{ "@@locale": "en", - "title": "Stocks", + "title": "Title", "@title": { - "description": "Title for the Stocks application" + "description": "Title for the application" } }'''; const String arbFileWithZhLocale = '''{ "@@locale": "zh", - "title": "Stocks", + "title": "标题", "@title": { - "description": "Title for the Stocks application" + "description": "Title for the application" } }'''; @@ -314,7 +383,7 @@ void main() { const String arbFileWithZhLocale = '''{ "@@locale": "zh", - "title": "Stocks", + "title": "标题", "@title": { "description": "Title for the Stocks application" } @@ -430,10 +499,10 @@ void main() { generator.classMethods.first, ''' String get title { return Intl.message( - r'Stocks', + r'Title', locale: _localeName, name: 'title', - desc: r'Title for the Stocks application' + desc: r'Title for the application' ); } ''');