Add locale-specific DateTime formatting syntax (#129573)
Based on the [message format syntax](https://unicode-org.github.io/icu/userguide/format_parse/messages/#examples) for [ICU4J](https://unicode-org.github.io/icu-docs/apidoc/released/icu4j/com/ibm/icu/text/MessageFormat.html). This adds new syntax to the current Flutter messageFormat parser which should allow developers to add locale-specific date formatting. ## Usage example ``` "datetimeTest": "Today is {today, date, ::yMd}", "@datetimeTest": { "placeholders": { "today": { "description": "The date placeholder", "type": "DateTime" } } } ``` compiles to ``` String datetimeTest(DateTime today) { String _temp0 = intl.DateFormat.yMd(localeName).format(today); return 'Today is $_temp0'; } ``` Fixes https://github.com/flutter/flutter/issues/127304.
This commit is contained in:
@@ -1759,6 +1759,67 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
|
||||
});
|
||||
});
|
||||
|
||||
group('argument messages', () {
|
||||
testWithoutContext('should generate proper calls to intl.DateFormat', () {
|
||||
setupLocalizations(<String, String>{
|
||||
'en': '''
|
||||
{
|
||||
"datetime": "{today, date, ::yMd}"
|
||||
}'''
|
||||
});
|
||||
expect(getGeneratedFileContent(locale: 'en'), contains('intl.DateFormat.yMd(localeName).format(today)'));
|
||||
});
|
||||
|
||||
testWithoutContext('should generate proper calls to intl.DateFormat when using time', () {
|
||||
setupLocalizations(<String, String>{
|
||||
'en': '''
|
||||
{
|
||||
"datetime": "{current, time, ::jms}"
|
||||
}'''
|
||||
});
|
||||
expect(getGeneratedFileContent(locale: 'en'), contains('intl.DateFormat.jms(localeName).format(current)'));
|
||||
});
|
||||
|
||||
testWithoutContext('should not complain when placeholders are explicitly typed to DateTime', () {
|
||||
setupLocalizations(<String, String>{
|
||||
'en': '''
|
||||
{
|
||||
"datetime": "{today, date, ::yMd}",
|
||||
"@datetime": {
|
||||
"placeholders": {
|
||||
"today": { "type": "DateTime" }
|
||||
}
|
||||
}
|
||||
}'''
|
||||
});
|
||||
expect(getGeneratedFileContent(locale: 'en'), contains('String datetime(DateTime today) {'));
|
||||
});
|
||||
|
||||
testWithoutContext('should automatically infer date time placeholders that are not explicitly defined', () {
|
||||
setupLocalizations(<String, String>{
|
||||
'en': '''
|
||||
{
|
||||
"datetime": "{today, date, ::yMd}"
|
||||
}'''
|
||||
});
|
||||
expect(getGeneratedFileContent(locale: 'en'), contains('String datetime(DateTime today) {'));
|
||||
});
|
||||
|
||||
testWithoutContext('should throw on invalid DateFormat', () {
|
||||
try {
|
||||
setupLocalizations(<String, String>{
|
||||
'en': '''
|
||||
{
|
||||
"datetime": "{today, date, ::yMMMMMd}"
|
||||
}'''
|
||||
});
|
||||
assert(false);
|
||||
} on L10nException {
|
||||
expect(logger.errorText, contains('Date format "yMMMMMd" for placeholder today does not have a corresponding DateFormat constructor'));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// All error handling for messages should collect errors on a per-error
|
||||
// basis and log them out individually. Then, it will throw an L10nException.
|
||||
group('error handling tests', () {
|
||||
|
||||
@@ -306,6 +306,25 @@ void main() {
|
||||
])
|
||||
));
|
||||
|
||||
expect(Parser('argumentTest', 'app_en.arb', 'Today is {date, date, ::yMMd}').parse(), equals(
|
||||
Node(ST.message, 0, children: <Node>[
|
||||
Node(ST.string, 0, value: 'Today is '),
|
||||
Node(ST.argumentExpr, 9, children: <Node>[
|
||||
Node(ST.openBrace, 9, value: '{'),
|
||||
Node(ST.identifier, 10, value: 'date'),
|
||||
Node(ST.comma, 14, value: ','),
|
||||
Node(ST.argType, 16, children: <Node>[
|
||||
Node(ST.date, 16, value: 'date'),
|
||||
]),
|
||||
Node(ST.comma, 20, value: ','),
|
||||
Node(ST.colon, 22, value: ':'),
|
||||
Node(ST.colon, 23, value: ':'),
|
||||
Node(ST.identifier, 24, value: 'yMMd'),
|
||||
Node(ST.closeBrace, 28, value: '}'),
|
||||
]),
|
||||
])
|
||||
));
|
||||
|
||||
expect(Parser(
|
||||
'plural',
|
||||
'app_en.arb',
|
||||
|
||||
@@ -126,46 +126,49 @@ void main() {
|
||||
'#l10n 73 (he)\n'
|
||||
'#l10n 74 (they)\n'
|
||||
'#l10n 75 (she)\n'
|
||||
'#l10n 76 (--- es ---)\n'
|
||||
'#l10n 77 (ES - Hello world)\n'
|
||||
'#l10n 78 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 79 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 80 (ES - Hello Mundo)\n'
|
||||
'#l10n 81 (ES - Hola Mundo)\n'
|
||||
'#l10n 82 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 83 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 84 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 85 (ES - Hello for 123)\n'
|
||||
'#l10n 86 (ES - Hello)\n'
|
||||
'#l10n 87 (ES - Hello World)\n'
|
||||
'#l10n 88 (ES - Hello two worlds)\n'
|
||||
'#l10n 89 (ES - Hello)\n'
|
||||
'#l10n 90 (ES - Hello nuevo World)\n'
|
||||
'#l10n 91 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 92 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 93 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 94 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 95 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 96 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 97 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 98 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 99 (ES - \$!)\n'
|
||||
'#l10n 100 (ES - One \$)\n'
|
||||
"#l10n 101 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 102 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 103 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 104 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 105 (ES - 16 wheel truck)\n'
|
||||
"#l10n 106 (ES - Sedan's elegance)\n"
|
||||
'#l10n 107 (ES - Cabriolet has "acceleration")\n'
|
||||
'#l10n 108 (ES - Oh, she found ES - 1 itemES - !)\n'
|
||||
'#l10n 109 (ES - Indeed, ES - they like ES - Flutter!)\n'
|
||||
'#l10n 110 (--- es_419 ---)\n'
|
||||
'#l10n 111 (ES 419 - Hello World)\n'
|
||||
'#l10n 112 (ES 419 - Hello)\n'
|
||||
'#l10n 76 (6/26/2023)\n'
|
||||
'#l10n 77 (5:23:00 AM)\n'
|
||||
'#l10n 78 (--- es ---)\n'
|
||||
'#l10n 79 (ES - Hello world)\n'
|
||||
'#l10n 80 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 81 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 82 (ES - Hello Mundo)\n'
|
||||
'#l10n 83 (ES - Hola Mundo)\n'
|
||||
'#l10n 84 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 85 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 86 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 87 (ES - Hello for 123)\n'
|
||||
'#l10n 88 (ES - Hello)\n'
|
||||
'#l10n 89 (ES - Hello World)\n'
|
||||
'#l10n 90 (ES - Hello two worlds)\n'
|
||||
'#l10n 91 (ES - Hello)\n'
|
||||
'#l10n 92 (ES - Hello nuevo World)\n'
|
||||
'#l10n 93 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 94 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 95 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 96 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 97 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 98 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 99 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 100 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 101 (ES - \$!)\n'
|
||||
'#l10n 102 (ES - One \$)\n'
|
||||
"#l10n 103 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 104 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 105 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 106 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 107 (ES - 16 wheel truck)\n'
|
||||
"#l10n 108 (ES - Sedan's elegance)\n"
|
||||
'#l10n 109 (ES - Cabriolet has "acceleration")\n'
|
||||
'#l10n 110 (ES - Oh, she found ES - 1 itemES - !)\n'
|
||||
'#l10n 111 (ES - Indeed, ES - they like ES - Flutter!)\n'
|
||||
'#l10n 112 (--- es_419 ---)\n'
|
||||
'#l10n 113 (ES 419 - Hello World)\n'
|
||||
'#l10n 114 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n 114 (ES 419 - Hello)\n'
|
||||
'#l10n 115 (ES 419 - Hello World)\n'
|
||||
'#l10n 116 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n END\n'
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -232,6 +232,8 @@ class Home extends StatelessWidget {
|
||||
"${localizations.selectInPlural('male', 1)}",
|
||||
"${localizations.selectInPlural('male', 2)}",
|
||||
"${localizations.selectInPlural('female', 1)}",
|
||||
'${localizations.datetime1(DateTime(2023, 6, 26))}',
|
||||
'${localizations.datetime2(DateTime(2023, 6, 26, 5, 23))}',
|
||||
]);
|
||||
},
|
||||
),
|
||||
@@ -682,7 +684,9 @@ void main() {
|
||||
"type": "num"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"datetime1": "{today, date, ::yMd}",
|
||||
"datetime2": "{current, time, ::jms}"
|
||||
}
|
||||
''';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user