Skip to content

Commit a396cc3

Browse files
AlexV525thkim1011
andauthored
Allow select cases to be numbers (flutter#116625) (flutter#120847)
(cherry picked from commit 583a812) Co-authored-by: Tae Hyung Kim <thkim1011@users.noreply.github.com>
1 parent c7c2f20 commit a396cc3

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

packages/flutter_tools/lib/src/localizations/message_parser.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ Map<ST, List<List<ST>>> grammar = <ST, List<List<ST>>>{
7070
],
7171
ST.selectPart: <List<ST>>[
7272
<ST>[ST.identifier, ST.openBrace, ST.message, ST.closeBrace],
73+
<ST>[ST.number, ST.openBrace, ST.message, ST.closeBrace],
7374
<ST>[ST.other, ST.openBrace, ST.message, ST.closeBrace],
7475
],
7576
};
@@ -415,6 +416,7 @@ class Parser {
415416
case ST.selectParts:
416417
if (tokens.isNotEmpty && (
417418
tokens[0].type == ST.identifier ||
419+
tokens[0].type == ST.number ||
418420
tokens[0].type == ST.other
419421
)) {
420422
parseAndConstructNode(ST.selectParts, 0);
@@ -425,8 +427,10 @@ class Parser {
425427
case ST.selectPart:
426428
if (tokens.isNotEmpty && tokens[0].type == ST.identifier) {
427429
parseAndConstructNode(ST.selectPart, 0);
428-
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
430+
} else if (tokens.isNotEmpty && tokens[0].type == ST.number) {
429431
parseAndConstructNode(ST.selectPart, 1);
432+
} else if (tokens.isNotEmpty && tokens[0].type == ST.other) {
433+
parseAndConstructNode(ST.selectPart, 2);
430434
} else {
431435
throw L10nParserException(
432436
'ICU Syntax Error: Select parts must be of the form "identifier { message }"',
@@ -588,6 +592,10 @@ class Parser {
588592
checkExtraRules(syntaxTree);
589593
return syntaxTree;
590594
} on L10nParserException catch (error) {
595+
// For debugging purposes.
596+
if (logger == null) {
597+
rethrow;
598+
}
591599
logger?.printError(error.toString());
592600
return Node(ST.empty, 0, value: '');
593601
}

packages/flutter_tools/test/general.shard/message_parser_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,15 @@ void main() {
519519
contains(expectedError3),
520520
)));
521521
});
522+
523+
testWithoutContext('parser allows select cases with numbers', () {
524+
final Node node = Parser('numberSelect', 'app_en.arb', '{ count, select, 0{none} 100{perfect} other{required!} }').parse();
525+
final Node selectExpr = node.children[0];
526+
final Node selectParts = selectExpr.children[5];
527+
final Node selectPart = selectParts.children[0];
528+
expect(selectPart.children[0].value, equals('0'));
529+
expect(selectPart.children[1].value, equals('{'));
530+
expect(selectPart.children[2].type, equals(ST.message));
531+
expect(selectPart.children[3].value, equals('}'));
532+
});
522533
}

0 commit comments

Comments
 (0)