From e8b3fe8aa667d655cd8fd4c432bea6314fb3c7a7 Mon Sep 17 00:00:00 2001 From: Shueh Chou Lu Date: Thu, 11 Nov 2021 00:47:25 +0800 Subject: [PATCH] Use num on plural (#93228) --- .../lib/src/localizations/gen_l10n.dart | 4 +- .../generate_localizations_test.dart | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart index d2a48856ea..9857062bd0 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart @@ -87,7 +87,7 @@ List generateMethodParameters(Message message) { assert(message.placeholders.isNotEmpty); final Placeholder? countPlaceholder = message.isPlural ? message.getCountPlaceholder() : null; return message.placeholders.map((Placeholder placeholder) { - final String? type = placeholder == countPlaceholder ? 'int' : placeholder.type; + final String? type = placeholder == countPlaceholder ? 'num' : placeholder.type; return '$type ${placeholder.name}'; }).toList(); } @@ -226,7 +226,7 @@ String _generatePluralMethod(Message message, String translationForMessage) { } final List parameters = message.placeholders.map((Placeholder placeholder) { - final String? placeholderType = placeholder == countPlaceholder ? 'int' : placeholder.type; + final String? placeholderType = placeholder == countPlaceholder ? 'num' : placeholder.type; return '$placeholderType ${placeholder.name}'; }).toList(); diff --git a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart index 12b75915ba..e850ca3fb4 100644 --- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart +++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart @@ -2708,4 +2708,47 @@ String orderNumber(int number) { AppLocalizations lookupAppLocalizations(Locale locale) { ''')); }); + + // Regression test for https://github.com/flutter/flutter/pull/93228 + testWithoutContext('should use num type for plural', () { + const String arbFile = ''' +{ + "tryToPollute": "{count, plural, =0{零} =1{一} other{其他}}", + "@tryToPollute": { + "placeholders": { + "count": { + "type": "int" + } + } + }, + "withoutType": "{count, plural, =0{零} =1{一} other{其他}}", + "@withoutType": { + "placeholders": { + "count": {} + } + } +}'''; + + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + l10nDirectory.childFile(defaultTemplateArbFileName) + .writeAsStringSync(arbFile); + + LocalizationsGenerator( + fileSystem: fs, + inputPathString: defaultL10nPathString, + outputPathString: defaultL10nPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + ) + ..loadResources() + ..writeOutputFiles(BufferLogger.test()); + + final String localizationsFile = fs.file( + fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'), + ).readAsStringSync(); + expect(localizationsFile, containsIgnoringWhitespace(r'String tryToPollute(num count) {')); + expect(localizationsFile, containsIgnoringWhitespace(r'String withoutType(num count) {')); + }); }