From 917577b808f77af8e25cce0099bff5c7ac65fd53 Mon Sep 17 00:00:00 2001 From: Shi-Hao Hong Date: Tue, 2 Feb 2021 17:06:05 -0800 Subject: [PATCH] [gen-l10n] Fixes named and positional parameter issue with NumberFormat when type is specified (#75209) --- .../lib/src/localizations/gen_l10n.dart | 21 +++- .../src/localizations/gen_l10n_templates.dart | 7 +- .../test/integration.shard/gen_l10n_test.dart | 113 ++++++++++-------- .../test_data/gen_l10n_project.dart | 100 ++++++++++++++++ 4 files changed, 182 insertions(+), 59 deletions(-) diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart index 45663543ee..3df003bf32 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart @@ -146,13 +146,24 @@ String generateNumberFormattingLogic(Message message) { } final Iterable parameters = placeholder.optionalParameters.map((OptionalParameter parameter) { - return '${parameter.name}: ${parameter.value}'; + if (parameter.value is num) { + return '${parameter.name}: ${parameter.value}'; + } else { + return '${parameter.name}: ${generateString(parameter.value.toString())}'; + } }, ); - return numberFormatTemplate - .replaceAll('@(placeholder)', placeholder.name) - .replaceAll('@(format)', placeholder.format) - .replaceAll('@(parameters)', parameters.join(', \n')); + + if (placeholder.hasNumberFormatWithParameters) { + return numberFormatNamedTemplate + .replaceAll('@(placeholder)', placeholder.name) + .replaceAll('@(format)', placeholder.format) + .replaceAll('@(parameters)', parameters.join(',\n ')); + } else { + return numberFormatPositionalTemplate + .replaceAll('@(placeholder)', placeholder.name) + .replaceAll('@(format)', placeholder.format); + } }); return formatStatements.isEmpty ? '@(none)' : formatStatements.join(''); diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart index 4bd85cd0a8..492a41770e 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n_templates.dart @@ -112,7 +112,12 @@ abstract class @(class) { @(delegateClass) '''; -const String numberFormatTemplate = ''' +const String numberFormatPositionalTemplate = ''' + final intl.NumberFormat @(placeholder)NumberFormat = intl.NumberFormat.@(format)(localeName); + final String @(placeholder)String = @(placeholder)NumberFormat.format(@(placeholder)); +'''; + +const String numberFormatNamedTemplate = ''' final intl.NumberFormat @(placeholder)NumberFormat = intl.NumberFormat.@(format)( locale: localeName, @(parameters) diff --git a/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart b/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart index a77341da0e..a1bd9d102e 100644 --- a/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart +++ b/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart @@ -92,59 +92,66 @@ void main() { '#l10n 37 (Hello World from 1960 to 2020)\n' '#l10n 38 (Hello for 123)\n' '#l10n 39 (Hello for price USD123.00)\n' - '#l10n 40 (Hello)\n' - '#l10n 41 (Hello World)\n' - '#l10n 42 (Hello two worlds)\n' - '#l10n 43 (Hello)\n' - '#l10n 44 (Hello new World)\n' - '#l10n 45 (Hello two new worlds)\n' - '#l10n 46 (Hello on Friday, January 1, 1960)\n' - '#l10n 47 (Hello World, on Friday, January 1, 1960)\n' - '#l10n 48 (Hello two worlds, on Friday, January 1, 1960)\n' - '#l10n 49 (Hello other 0 worlds, with a total of 100 citizens)\n' - '#l10n 50 (Hello World of 101 citizens)\n' - '#l10n 51 (Hello two worlds with 102 total citizens)\n' - '#l10n 52 ([Hello] -World- #123#)\n' - '#l10n 53 (\$!)\n' - '#l10n 54 (One \$)\n' - '#l10n 55 (Flutter\'s amazing!)\n' - '#l10n 56 (Flutter\'s amazing, times 2!)\n' - '#l10n 57 (Flutter is "amazing"!)\n' - '#l10n 58 (Flutter is "amazing", times 2!)\n' - '#l10n 59 (--- es ---)\n' - '#l10n 60 (ES - Hello world)\n' - '#l10n 61 (ES - Hello _NEWLINE_ World)\n' - '#l10n 62 (ES - Hola \$ Mundo)\n' - '#l10n 63 (ES - Hello Mundo)\n' - '#l10n 64 (ES - Hola Mundo)\n' - '#l10n 65 (ES - Hello World on viernes, 1 de enero de 1960)\n' - '#l10n 66 (ES - Hello world argument on 1/1/1960 at 0:00)\n' - '#l10n 67 (ES - Hello World from 1960 to 2020)\n' - '#l10n 68 (ES - Hello for 123)\n' - '#l10n 69 (ES - Hello)\n' - '#l10n 70 (ES - Hello World)\n' - '#l10n 71 (ES - Hello two worlds)\n' - '#l10n 72 (ES - Hello)\n' - '#l10n 73 (ES - Hello nuevo World)\n' - '#l10n 74 (ES - Hello two nuevo worlds)\n' - '#l10n 75 (ES - Hello on viernes, 1 de enero de 1960)\n' - '#l10n 76 (ES - Hello World, on viernes, 1 de enero de 1960)\n' - '#l10n 77 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n' - '#l10n 78 (ES - Hello other 0 worlds, with a total of 100 citizens)\n' - '#l10n 79 (ES - Hello World of 101 citizens)\n' - '#l10n 80 (ES - Hello two worlds with 102 total citizens)\n' - '#l10n 81 (ES - [Hola] -Mundo- #123#)\n' - '#l10n 82 (ES - \$!)\n' - '#l10n 83 (ES - One \$)\n' - '#l10n 84 (ES - Flutter\'s amazing!)\n' - '#l10n 85 (ES - Flutter\'s amazing, times 2!)\n' - '#l10n 86 (ES - Flutter is "amazing"!)\n' - '#l10n 87 (ES - Flutter is "amazing", times 2!)\n' - '#l10n 88 (--- es_419 ---)\n' - '#l10n 89 (ES 419 - Hello World)\n' - '#l10n 90 (ES 419 - Hello)\n' - '#l10n 91 (ES 419 - Hello World)\n' - '#l10n 92 (ES 419 - Hello two worlds)\n' + '#l10n 40 (Hello for price BTC0.50 (with optional param))\n' + '#l10n 41 (Hello for price BTC\'0.50 (with special character))\n' + '#l10n 42 (Hello for price BTC"0.50 (with special character))\n' + '#l10n 43 (Hello for price BTC"\'0.50 (with special character))\n' + '#l10n 44 (Hello for Decimal Pattern 1,200,000)\n' + '#l10n 45 (Hello for Percent Pattern 120,000,000%)\n' + '#l10n 46 (Hello for Scientific Pattern 1E6)\n' + '#l10n 47 (Hello)\n' + '#l10n 48 (Hello World)\n' + '#l10n 49 (Hello two worlds)\n' + '#l10n 50 (Hello)\n' + '#l10n 51 (Hello new World)\n' + '#l10n 52 (Hello two new worlds)\n' + '#l10n 53 (Hello on Friday, January 1, 1960)\n' + '#l10n 54 (Hello World, on Friday, January 1, 1960)\n' + '#l10n 55 (Hello two worlds, on Friday, January 1, 1960)\n' + '#l10n 56 (Hello other 0 worlds, with a total of 100 citizens)\n' + '#l10n 57 (Hello World of 101 citizens)\n' + '#l10n 58 (Hello two worlds with 102 total citizens)\n' + '#l10n 59 ([Hello] -World- #123#)\n' + '#l10n 60 (\$!)\n' + '#l10n 61 (One \$)\n' + '#l10n 62 (Flutter\'s amazing!)\n' + '#l10n 63 (Flutter\'s amazing, times 2!)\n' + '#l10n 64 (Flutter is "amazing"!)\n' + '#l10n 65 (Flutter is "amazing", times 2!)\n' + '#l10n 66 (--- es ---)\n' + '#l10n 67 (ES - Hello world)\n' + '#l10n 68 (ES - Hello _NEWLINE_ World)\n' + '#l10n 69 (ES - Hola \$ Mundo)\n' + '#l10n 70 (ES - Hello Mundo)\n' + '#l10n 71 (ES - Hola Mundo)\n' + '#l10n 72 (ES - Hello World on viernes, 1 de enero de 1960)\n' + '#l10n 73 (ES - Hello world argument on 1/1/1960 at 0:00)\n' + '#l10n 74 (ES - Hello World from 1960 to 2020)\n' + '#l10n 75 (ES - Hello for 123)\n' + '#l10n 76 (ES - Hello)\n' + '#l10n 77 (ES - Hello World)\n' + '#l10n 78 (ES - Hello two worlds)\n' + '#l10n 79 (ES - Hello)\n' + '#l10n 80 (ES - Hello nuevo World)\n' + '#l10n 81 (ES - Hello two nuevo worlds)\n' + '#l10n 82 (ES - Hello on viernes, 1 de enero de 1960)\n' + '#l10n 83 (ES - Hello World, o n viernes, 1 de enero de 1960)\n' + '#l10n 84 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n' + '#l10n 85 (ES - Hello other 0 worlds, with a total of 100 citizens)\n' + '#l10n 86 (ES - Hello World of 101 citizens)\n' + '#l10n 87 (ES - Hello two worlds with 102 total citizens)\n' + '#l10n 88 (ES - [Hola] -Mundo- #123#)\n' + '#l10n 89 (ES - \$!)\n' + '#l10n 90 (ES - One \$)\n' + '#l10n 91 (ES - Flutter\'s amazing!)\n' + '#l10n 92 (ES - Flutter\'s amazing, times 2!)\n' + '#l10n 93 (ES - Flutter is "amazing"!)\n' + '#l10n 94 (ES - Flutter is "amazing", times 2!)\n' + '#l10n 95 (--- es_419 ---)\n' + '#l10n 96 (ES 419 - Hello World)\n' + '#l10n 97 (ES 419 - Hello)\n' + '#l10n 98 (ES 419 - Hello World)\n' + '#l10n 99 (ES 419 - Hello two worlds)\n' '#l10n END\n' ); } diff --git a/packages/flutter_tools/test/integration.shard/test_data/gen_l10n_project.dart b/packages/flutter_tools/test/integration.shard/test_data/gen_l10n_project.dart index 45265a043d..3a0c1ab6f0 100644 --- a/packages/flutter_tools/test/integration.shard/test_data/gen_l10n_project.dart +++ b/packages/flutter_tools/test/integration.shard/test_data/gen_l10n_project.dart @@ -199,6 +199,13 @@ class Home extends StatelessWidget { '${localizations.helloWorldDuring(DateTime(1960), DateTime(2020))}', '${localizations.helloFor(123)}', '${localizations.helloCost("price", 123)}', + '${localizations.helloCostWithOptionalParam("price", .5)}', + '${localizations.helloCostWithSpecialCharacter1("price", .5)}', + '${localizations.helloCostWithSpecialCharacter2("price", .5)}', + '${localizations.helloCostWithSpecialCharacter3("price", .5)}', + '${localizations.helloDecimalPattern(1200000)}', + '${localizations.helloPercentPattern(1200000)}', + '${localizations.helloScientificPattern(1200000)}', '${localizations.helloWorlds(0)}', '${localizations.helloWorlds(1)}', '${localizations.helloWorlds(2)}', @@ -411,6 +418,99 @@ void main() { } }, + "helloCostWithOptionalParam": "Hello for {price} {value} (with optional param)", + "@helloCostWithOptionalParam": { + "description": "A message with string and int (currency) parameters", + "placeholders": { + "price": {}, + "value": { + "type": "double", + "format": "currency", + "optionalParameters": { + "name": "BTC" + } + } + } + }, + + "helloCostWithSpecialCharacter1": "Hello for {price} {value} (with special character)", + "@helloCostWithSpecialCharacter1": { + "description": "A message with string and int (currency) parameters", + "placeholders": { + "price": {}, + "value": { + "type": "double", + "format": "currency", + "optionalParameters": { + "name": "BTC'" + } + } + } + }, + + "helloCostWithSpecialCharacter2": "Hello for {price} {value} (with special character)", + "@helloCostWithSpecialCharacter2": { + "description": "A message with string and int (currency) parameters", + "placeholders": { + "price": {}, + "value": { + "type": "double", + "format": "currency", + "optionalParameters": { + "name": "BTC\"" + } + } + } + }, + + "helloCostWithSpecialCharacter3": "Hello for {price} {value} (with special character)", + "@helloCostWithSpecialCharacter3": { + "description": "A message with string and int (currency) parameters", + "placeholders": { + "price": {}, + "value": { + "type": "double", + "format": "currency", + "optionalParameters": { + "name": "BTC\"'" + } + } + } + }, + + "helloDecimalPattern": "Hello for Decimal Pattern {value}", + "@helloDecimalPattern": { + "description": "A message which displays a number in decimal pattern", + "placeholders": { + "value": { + "type": "double", + "format": "decimalPattern" + } + } + }, + + "helloPercentPattern": "Hello for Percent Pattern {value}", + "@helloPercentPattern": { + "description": "A message which displays a number in percent pattern", + "placeholders": { + "value": { + "type": "double", + "format": "percentPattern" + } + } + }, + + "helloScientificPattern": "Hello for Scientific Pattern {value}", + "@helloScientificPattern": { + "description": "A message which displays scientific notation of a number", + "placeholders": { + "value": { + "type": "double", + "format": "scientificPattern" + } + } + }, + "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}}", "@helloWorlds": { "description": "A plural message",