Unverified Commit 2eed59c1 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

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
parent 6a5ef29a
...@@ -92,7 +92,7 @@ Future<void> main(List<String> arguments) async { ...@@ -92,7 +92,7 @@ Future<void> main(List<String> arguments) async {
'--output-dir=${localizationsGenerator.l10nDirectory.path}', '--output-dir=${localizationsGenerator.l10nDirectory.path}',
'--no-use-deferred-loading', '--no-use-deferred-loading',
localizationsGenerator.outputFile.path, localizationsGenerator.outputFile.path,
...localizationsGenerator.arbFilenames, ...localizationsGenerator.arbPathStrings,
]); ]);
if (generateFromArbResult.exitCode != 0) { if (generateFromArbResult.exitCode != 0) {
stderr.write(generateFromArbResult.stderr); stderr.write(generateFromArbResult.stderr);
......
...@@ -359,8 +359,8 @@ class LocalizationsGenerator { ...@@ -359,8 +359,8 @@ class LocalizationsGenerator {
/// Sets the [className] for the localizations and localizations delegate /// Sets the [className] for the localizations and localizations delegate
/// classes. /// classes.
/// The list of all arb files in [l10nDirectory]. /// The list of all arb path strings in [l10nDirectory].
final List<String> arbFilenames = <String>[]; final List<String> arbPathStrings = <String>[];
/// The supported language codes as found in the arb files located in /// The supported language codes as found in the arb files located in
/// [l10nDirectory]. /// [l10nDirectory].
...@@ -477,7 +477,7 @@ class LocalizationsGenerator { ...@@ -477,7 +477,7 @@ class LocalizationsGenerator {
localeString = arbFilenameLocaleRE.firstMatch(filePath)[1]; localeString = arbFilenameLocaleRE.firstMatch(filePath)[1];
} }
arbFilenames.add(filePath); arbPathStrings.add(filePath);
final LocaleInfo localeInfo = LocaleInfo.fromString(localeString); final LocaleInfo localeInfo = LocaleInfo.fromString(localeString);
if (localeInfoList.contains(localeInfo)) if (localeInfoList.contains(localeInfo))
throw L10nException( throw L10nException(
...@@ -488,6 +488,7 @@ class LocalizationsGenerator { ...@@ -488,6 +488,7 @@ class LocalizationsGenerator {
} }
} }
arbPathStrings.sort();
localeInfoList.sort(); localeInfoList.sort();
supportedLocales.addAll(localeInfoList); supportedLocales.addAll(localeInfoList);
supportedLanguageCodes.addAll(localeInfoList.map((LocaleInfo localeInfo) { supportedLanguageCodes.addAll(localeInfoList.map((LocaleInfo localeInfo) {
......
...@@ -18,15 +18,18 @@ const String defaultTemplateArbFileName = 'app_en_US.arb'; ...@@ -18,15 +18,18 @@ const String defaultTemplateArbFileName = 'app_en_US.arb';
const String defaultOutputFileString = 'output-localization-file'; const String defaultOutputFileString = 'output-localization-file';
const String defaultClassNameString = 'AppLocalizations'; const String defaultClassNameString = 'AppLocalizations';
const String singleMessageArbFileString = '''{ const String singleMessageArbFileString = '''{
"title": "Stocks", "title": "Title",
"@title": { "@title": {
"description": "Title for the Stocks application" "description": "Title for the application"
} }
}'''; }''';
const String esArbFileName = 'app_es.arb'; const String esArbFileName = 'app_es.arb';
const String singleEsMessageArbFileString = '''{ const String singleEsMessageArbFileString = '''{
"title": "Acciones" "title": "Título"
}''';
const String singleZhMessageArbFileString = '''{
"title": "标题"
}'''; }''';
void _standardFlutterDirectoryL10nSetup(FileSystem fs) { void _standardFlutterDirectoryL10nSetup(FileSystem fs) {
...@@ -261,20 +264,86 @@ void main() { ...@@ -261,20 +264,86 @@ void main() {
expect(generator.supportedLocales.contains(LocaleInfo.fromString('es')), true); 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', () { test('correctly parses @@locale property in arb file', () {
const String arbFileWithEnLocale = '''{ const String arbFileWithEnLocale = '''{
"@@locale": "en", "@@locale": "en",
"title": "Stocks", "title": "Title",
"@title": { "@title": {
"description": "Title for the Stocks application" "description": "Title for the application"
} }
}'''; }''';
const String arbFileWithZhLocale = '''{ const String arbFileWithZhLocale = '''{
"@@locale": "zh", "@@locale": "zh",
"title": "Stocks", "title": "标题",
"@title": { "@title": {
"description": "Title for the Stocks application" "description": "Title for the application"
} }
}'''; }''';
...@@ -314,7 +383,7 @@ void main() { ...@@ -314,7 +383,7 @@ void main() {
const String arbFileWithZhLocale = '''{ const String arbFileWithZhLocale = '''{
"@@locale": "zh", "@@locale": "zh",
"title": "Stocks", "title": "标题",
"@title": { "@title": {
"description": "Title for the Stocks application" "description": "Title for the Stocks application"
} }
...@@ -430,10 +499,10 @@ void main() { ...@@ -430,10 +499,10 @@ void main() {
generator.classMethods.first, generator.classMethods.first,
''' String get title { ''' String get title {
return Intl.message( return Intl.message(
r'Stocks', r'Title',
locale: _localeName, locale: _localeName,
name: 'title', name: 'title',
desc: r'Title for the Stocks application' desc: r'Title for the application'
); );
} }
'''); ''');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment