@@ -74,6 +74,7 @@ Future<LocalizationsGenerator> generateLocalizations({
7474 suppressWarnings: options.suppressWarnings,
7575 useRelaxedSyntax: options.relaxSyntax,
7676 useNamedParameters: options.useNamedParameters,
77+ useExplicitPluralValues: options.useExplicitPluralValues,
7778 )
7879 ..loadResources ()
7980 ..writeOutputFiles (isFromYaml: true , useCRLF: useCRLF);
@@ -528,6 +529,7 @@ class LocalizationsGenerator {
528529 bool suppressWarnings = false ,
529530 bool useRelaxedSyntax = false ,
530531 bool useNamedParameters = false ,
532+ bool useExplicitPluralValues = false ,
531533 }) {
532534 final Directory projectDirectory = projectDirFromPath (fileSystem, projectPathString);
533535 final Directory inputDirectory = inputDirectoryFromPath (
@@ -567,6 +569,7 @@ class LocalizationsGenerator {
567569 suppressWarnings: suppressWarnings,
568570 useRelaxedSyntax: useRelaxedSyntax,
569571 useNamedParameters: useNamedParameters,
572+ useExplicitPluralValues: useExplicitPluralValues,
570573 );
571574 }
572575
@@ -593,6 +596,7 @@ class LocalizationsGenerator {
593596 this .suppressWarnings = false ,
594597 this .useRelaxedSyntax = false ,
595598 this .useNamedParameters = false ,
599+ this .useExplicitPluralValues = false ,
596600 });
597601
598602 final FileSystem _fs;
@@ -741,6 +745,14 @@ class LocalizationsGenerator {
741745 /// ```
742746 final bool useNamedParameters;
743747
748+ /// Whether explicit plural values such as `=1` match only that exact value.
749+ ///
750+ /// If this is true, an explicit `=N` plural case is compared against the
751+ /// count before the plural category (`zero` , `one` , `two` , `few` , `many` ,
752+ /// `other` ) is resolved, and any non-negative integer is allowed for `N` .
753+ /// Otherwise `=0` , `=1` and `=2` are treated as `zero` , `one` and `two` .
754+ final bool useExplicitPluralValues;
755+
744756 static bool _isNotReadable (FileStat fileStat) {
745757 final String rawStatString = fileStat.modeString ();
746758 // Removes potential prepended permission bits, such as '(suid)' and '(guid)'.
@@ -1297,6 +1309,8 @@ class LocalizationsGenerator {
12971309
12981310 case ST .pluralExpr:
12991311 final pluralLogicArgs = < String , String > {};
1312+ // Explicit values in reverse source order, keyed by their integer value.
1313+ final explicitPluralArgs = < int , String > {};
13001314 // Recall that pluralExpr are of the form
13011315 // pluralExpr := "{" ID "," "plural" "," pluralParts "}"
13021316 assert (node.children[1 ].type == ST .identifier);
@@ -1308,7 +1322,8 @@ class LocalizationsGenerator {
13081322 for (final Node pluralPart in pluralParts.children.reversed) {
13091323 String pluralCase;
13101324 Node pluralMessage;
1311- if (pluralPart.children[0 ].value == '=' ) {
1325+ final isExplicitValue = pluralPart.children[0 ].value == '=' ;
1326+ if (isExplicitValue) {
13121327 assert (pluralPart.children[1 ].type == ST .number);
13131328 assert (pluralPart.children[3 ].type == ST .message);
13141329 pluralCase = pluralPart.children[1 ].value! ;
@@ -1322,7 +1337,23 @@ class LocalizationsGenerator {
13221337 pluralCase = pluralPart.children[0 ].value! ;
13231338 pluralMessage = pluralPart.children[2 ];
13241339 }
1325- if (! pluralLogicArgs.containsKey (pluralCases[pluralCase])) {
1340+ final int ? explicitValue = useExplicitPluralValues && isExplicitValue
1341+ ? int .tryParse (pluralCase)
1342+ : null ;
1343+ if (useExplicitPluralValues && isExplicitValue && explicitValue == null ) {
1344+ throw L10nParserException (
1345+ '''
1346+ The plural case "=$pluralCase " is too large to be represented as an integer.''' ,
1347+ _inputFileNames[locale]! ,
1348+ message.resourceId,
1349+ translationForMessage,
1350+ pluralPart.positionInMessage,
1351+ );
1352+ }
1353+ if (explicitValue != null && ! explicitPluralArgs.containsKey (explicitValue)) {
1354+ explicitPluralArgs[explicitValue] = generateVariables (pluralMessage);
1355+ } else if (explicitValue == null &&
1356+ ! pluralLogicArgs.containsKey (pluralCases[pluralCase])) {
13261357 final String pluralPartExpression = generateVariables (pluralMessage);
13271358 final String ? transformedPluralCase = pluralCases[pluralCase];
13281359 // A valid plural case is one of "=0", "=1", "=2", "zero", "one", "two", "few", "many", or "other".
@@ -1351,7 +1382,19 @@ The plural cases must be one of "=0", "=1", "=2", "zero", "one", "two", "few", "
13511382 pluralVariableTemplate
13521383 .replaceAll ('@(varName)' , tempVarName)
13531384 .replaceAll ('@(count)' , identifier.value! )
1354- .replaceAll ('@(pluralLogicArgs)' , pluralLogicArgs.values.join ('\n ' )),
1385+ .replaceAll ('@(pluralLogicArgs)' , pluralLogicArgs.values.join ('\n ' ))
1386+ // Must run last and only once, so message text is never rewritten.
1387+ .replaceFirst (
1388+ '@(explicitPluralCases)' ,
1389+ explicitPluralArgs.entries
1390+ .toList ()
1391+ .reversed
1392+ .map (
1393+ (MapEntry <int , String > entry) =>
1394+ '${identifier .value !} == ${entry .key }\n ? ${entry .value }\n : ' ,
1395+ )
1396+ .join (),
1397+ ),
13551398 );
13561399 return '\$ $tempVarName ' ;
13571400
0 commit comments