Skip to content

Commit

Permalink
Throw error when plural case had undefined behavior (#116622)
Browse files Browse the repository at this point in the history
* init

* add comment

* make error more actionable
  • Loading branch information
thkim1011 committed Dec 8, 2022
1 parent bebea0c commit 117a83a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/flutter_tools/lib/src/localizations/gen_l10n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,20 @@ class LocalizationsGenerator {
}
if (!pluralLogicArgs.containsKey(pluralCases[pluralCase])) {
final String pluralPartExpression = generateVariables(pluralMessage);
pluralLogicArgs[pluralCases[pluralCase]!] = ' ${pluralCases[pluralCase]}: $pluralPartExpression,';
final String? transformedPluralCase = pluralCases[pluralCase];
// A valid plural case is one of "=0", "=1", "=2", "zero", "one", "two", "few", "many", or "other".
if (transformedPluralCase == null) {
throw L10nParserException(
'''
The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "many", or "other.
$pluralCase is not a valid plural case.''',
_inputFileNames[locale]!,
message.resourceId,
translationForMessage,
pluralPart.positionInMessage,
);
}
pluralLogicArgs[transformedPluralCase] = ' ${pluralCases[pluralCase]}: $pluralPartExpression,';
} else if (!suppressWarnings) {
logger.printWarning('''
[${_inputFileNames[locale]}:${message.resourceId}] ICU Syntax Warning: The plural part specified below is overridden by a later plural part.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,38 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
^'''));
});

testWithoutContext('undefined plural cases throws syntax error', () {
const String pluralMessageWithUndefinedParts = '''
{
"count": "{count,plural, =0{None} =1{One} =2{Two} =3{Undefined Behavior!} other{Hmm...}}"
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithUndefinedParts);
try {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
} on L10nException catch (error) {
expect(error.message, contains('Found syntax errors.'));
expect(logger.hadErrorOutput, isTrue);
expect(logger.errorText, contains('''
[app_en.arb:count] The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "many", or "other.
3 is not a valid plural case.
{count,plural, =0{None} =1{One} =2{Two} =3{Undefined Behavior!} other{Hmm...}}
^'''));
}
});

testWithoutContext('should automatically infer plural placeholders that are not explicitly defined', () {
const String pluralMessageWithoutPlaceholdersAttribute = '''
{
Expand Down

0 comments on commit 117a83a

Please sign in to comment.