Unverified Commit 4e811d28 authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[gen_l10n] Fix plural parsing for translated messages (#53954)

parent c6ee4d26
...@@ -85,7 +85,7 @@ String generateNumberFormattingLogic(Message message) { ...@@ -85,7 +85,7 @@ String generateNumberFormattingLogic(Message message) {
return formatStatements.isEmpty ? '@(none)' : formatStatements.join(''); return formatStatements.isEmpty ? '@(none)' : formatStatements.join('');
} }
String generatePluralMethod(Message message) { String generatePluralMethod(Message message, AppResourceBundle bundle) {
if (message.placeholders.isEmpty) { if (message.placeholders.isEmpty) {
throw L10nException( throw L10nException(
'Unable to find placeholders for the plural message: ${message.resourceId}.\n' 'Unable to find placeholders for the plural message: ${message.resourceId}.\n'
...@@ -96,7 +96,7 @@ String generatePluralMethod(Message message) { ...@@ -96,7 +96,7 @@ String generatePluralMethod(Message message) {
// To make it easier to parse the plurals message, temporarily replace each // To make it easier to parse the plurals message, temporarily replace each
// "{placeholder}" parameter with "#placeholder#". // "{placeholder}" parameter with "#placeholder#".
String easyMessage = message.value; String easyMessage = bundle.translationFor(message);
for (final Placeholder placeholder in message.placeholders) for (final Placeholder placeholder in message.placeholders)
easyMessage = easyMessage.replaceAll('{${placeholder.name}}', '#${placeholder.name}#'); easyMessage = easyMessage.replaceAll('{${placeholder.name}}', '#${placeholder.name}#');
...@@ -168,7 +168,7 @@ String generateMethod(Message message, AppResourceBundle bundle) { ...@@ -168,7 +168,7 @@ String generateMethod(Message message, AppResourceBundle bundle) {
} }
if (message.isPlural) { if (message.isPlural) {
return generatePluralMethod(message); return generatePluralMethod(message, bundle);
} }
if (message.placeholdersRequireFormatting) { if (message.placeholdersRequireFormatting) {
......
...@@ -92,37 +92,40 @@ void main() { ...@@ -92,37 +92,40 @@ void main() {
'#l10n 13 (Hello GB fallback World)\n' '#l10n 13 (Hello GB fallback World)\n'
'#l10n 14 (--- zh ---)\n' '#l10n 14 (--- zh ---)\n'
'#l10n 15 (你好世界)\n' '#l10n 15 (你好世界)\n'
'#l10n 16 (--- scriptCode: zh_Hans ---)\n' '#l10n 16 (你好)\n'
'#l10n 17 (简体你好世界)\n' '#l10n 17 (你好世界)\n'
'#l10n 18 (--- scriptCode - zh_Hant ---)\n' '#l10n 18 (你好2个其他世界)\n'
'#l10n 19 (繁體你好世界)\n' '#l10n 19 (--- scriptCode: zh_Hans ---)\n'
'#l10n 20 (--- scriptCode - zh_Hant_TW ---)\n' '#l10n 20 (简体你好世界)\n'
'#l10n 21 (台灣繁體你好世界)\n' '#l10n 21 (--- scriptCode - zh_Hant ---)\n'
'#l10n 22 (--- General formatting tests ---)\n' '#l10n 22 (繁體你好世界)\n'
'#l10n 23 (Hello World)\n' '#l10n 23 (--- scriptCode - zh_Hant_TW ---)\n'
'#l10n 24 (Hello _NEWLINE_ World)\n' '#l10n 24 (台灣繁體你好世界)\n'
'#l10n 25 (Hello World)\n' '#l10n 25 (--- General formatting tests ---)\n'
'#l10n 26 (Hello World)\n' '#l10n 26 (Hello World)\n'
'#l10n 27 (Hello World on Friday, January 1, 1960)\n' '#l10n 27 (Hello _NEWLINE_ World)\n'
'#l10n 28 (Hello world argument on 1/1/1960 at 00:00)\n' '#l10n 28 (Hello World)\n'
'#l10n 29 (Hello World from 1960 to 2020)\n' '#l10n 29 (Hello World)\n'
'#l10n 30 (Hello for 123)\n' '#l10n 30 (Hello World on Friday, January 1, 1960)\n'
'#l10n 31 (Hello for price USD123.00)\n' '#l10n 31 (Hello world argument on 1/1/1960 at 00:00)\n'
'#l10n 32 (Hello)\n' '#l10n 32 (Hello World from 1960 to 2020)\n'
'#l10n 33 (Hello World)\n' '#l10n 33 (Hello for 123)\n'
'#l10n 34 (Hello two worlds)\n' '#l10n 34 (Hello for price USD123.00)\n'
'#l10n 35 (Hello)\n' '#l10n 35 (Hello)\n'
'#l10n 36 (Hello new World)\n' '#l10n 36 (Hello World)\n'
'#l10n 37 (Hello two new worlds)\n' '#l10n 37 (Hello two worlds)\n'
'#l10n 38 (Hello on Friday, January 1, 1960)\n' '#l10n 38 (Hello)\n'
'#l10n 39 (Hello World, on Friday, January 1, 1960)\n' '#l10n 39 (Hello new World)\n'
'#l10n 40 (Hello two worlds, on Friday, January 1, 1960)\n' '#l10n 40 (Hello two new worlds)\n'
'#l10n 41 (Hello other 0 worlds, with a total of 100 citizens)\n' '#l10n 41 (Hello on Friday, January 1, 1960)\n'
'#l10n 42 (Hello World of 101 citizens)\n' '#l10n 42 (Hello World, on Friday, January 1, 1960)\n'
'#l10n 43 (Hello two worlds with 102 total citizens)\n' '#l10n 43 (Hello two worlds, on Friday, January 1, 1960)\n'
'#l10n 44 ([Hello] -World- #123#)\n' '#l10n 44 (Hello other 0 worlds, with a total of 100 citizens)\n'
'#l10n 45 (Flutter\'s amazing!)\n' '#l10n 45 (Hello World of 101 citizens)\n'
'#l10n 46 (Flutter is "amazing"!)\n' '#l10n 46 (Hello two worlds with 102 total citizens)\n'
'#l10n 47 ([Hello] -World- #123#)\n'
'#l10n 48 (Flutter\'s amazing!)\n'
'#l10n 49 (Flutter is "amazing"!)\n'
'#l10n END\n' '#l10n END\n'
); );
}); });
......
...@@ -126,6 +126,9 @@ class Home extends StatelessWidget { ...@@ -126,6 +126,9 @@ class Home extends StatelessWidget {
callback: (BuildContext context) { callback: (BuildContext context) {
results.add('--- zh ---'); results.add('--- zh ---');
results.add(AppLocalizations.of(context).helloWorld); results.add(AppLocalizations.of(context).helloWorld);
results.add(AppLocalizations.of(context).helloWorlds(0));
results.add(AppLocalizations.of(context).helloWorlds(1));
results.add(AppLocalizations.of(context).helloWorlds(2));
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -402,13 +405,14 @@ void main() { ...@@ -402,13 +405,14 @@ void main() {
} }
'''; ''';
// Only tests `helloWorld`. The rest of the messages are added out of // Only tests `helloWorld` and `helloWorlds`. The rest of the messages
// necessity since every base class requires an override for every // are added out of necessity since every base class requires an
// message. // override for every message.
final String appZh = r''' final String appZh = r'''
{ {
"@@locale": "zh", "@@locale": "zh",
"helloWorld": "你好世界", "helloWorld": "你好世界",
"helloWorlds": "{count,plural, =0{你好} =1{你好世界} other{你好{count}个其他世界}}",
"helloNewlineWorld": "Hello \n World", "helloNewlineWorld": "Hello \n World",
"hello": "Hello {world}", "hello": "Hello {world}",
"greeting": "{hello} {world}", "greeting": "{hello} {world}",
...@@ -417,7 +421,6 @@ void main() { ...@@ -417,7 +421,6 @@ void main() {
"helloOn": "Hello {world} on {date} at {time}", "helloOn": "Hello {world} on {date} at {time}",
"helloFor": "Hello for {value}", "helloFor": "Hello for {value}",
"helloCost": "Hello for {price} {value}", "helloCost": "Hello for {price} {value}",
"helloWorlds": "{count,plural, =0{Hello} =1{Hello World} =2{Hello two worlds} few{Hello {count} worlds} many{Hello all {count} worlds} other{Hello other {count} worlds}}",
"helloAdjectiveWorlds": "{count,plural, =0{Hello} =1{Hello {adjective} World} =2{Hello two {adjective} worlds} other{Hello other {count} {adjective} worlds}}", "helloAdjectiveWorlds": "{count,plural, =0{Hello} =1{Hello {adjective} World} =2{Hello two {adjective} worlds} other{Hello other {count} {adjective} worlds}}",
"helloWorldsOn": "{count,plural, =0{Hello on {date}} =1{Hello World, on {date}} =2{Hello two worlds, on {date}} other{Hello other {count} worlds, on {date}}}", "helloWorldsOn": "{count,plural, =0{Hello on {date}} =1{Hello World, on {date}} =2{Hello two worlds, on {date}} other{Hello other {count} worlds, on {date}}}",
"helloWorldPopulation": "{count,plural, =1{Hello World of {population} citizens} =2{Hello two worlds with {population} total citizens} many{Hello all {count} worlds, with a total of {population} citizens} other{Hello other {count} worlds, with a total of {population} citizens}}", "helloWorldPopulation": "{count,plural, =1{Hello World of {population} citizens} =2{Hello two worlds with {population} total citizens} many{Hello all {count} worlds, with a total of {population} citizens} other{Hello other {count} worlds, with a total of {population} citizens}}",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment