From e8d29075954e58f410ab42d3c6481abd93970d1f Mon Sep 17 00:00:00 2001 From: Per Classon Date: Tue, 7 Apr 2020 21:21:02 +0200 Subject: [PATCH] [gen_l10n] Handle single, double quotes, and dollar signs in strings (#54185) --- dev/tools/localization/gen_l10n.dart | 8 ++-- .../test/integration.shard/gen_l10n_test.dart | 8 +++- .../test_data/gen_l10n_project.dart | 39 ++++++++++++++++++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/dev/tools/localization/gen_l10n.dart b/dev/tools/localization/gen_l10n.dart index 7120f68fd8..63196738b0 100644 --- a/dev/tools/localization/gen_l10n.dart +++ b/dev/tools/localization/gen_l10n.dart @@ -123,7 +123,7 @@ String generatePluralMethod(Message message, AppResourceBundle bundle) { final RegExp expRE = RegExp('($pluralKey)\\s*{([^}]+)}'); final RegExpMatch match = expRE.firstMatch(easyMessage); if (match != null && match.groupCount == 2) { - String argValue = match.group(2); + String argValue = generateString(match.group(2)); for (final Placeholder placeholder in message.placeholders) { if (placeholder != countPlaceholder && placeholder.requiresFormatting) { argValue = argValue.replaceAll('#${placeholder.name}#', '\${${placeholder.name}String}'); @@ -131,7 +131,7 @@ String generatePluralMethod(Message message, AppResourceBundle bundle) { argValue = argValue.replaceAll('#${placeholder.name}#', '\${${placeholder.name}}'); } } - pluralLogicArgs.add(" ${pluralIds[pluralKey]}: '$argValue'"); + pluralLogicArgs.add(' ${pluralIds[pluralKey]}: $argValue'); } } @@ -155,7 +155,7 @@ String generatePluralMethod(Message message, AppResourceBundle bundle) { String generateMethod(Message message, AppResourceBundle bundle) { String generateMessage() { - String messageValue = bundle.translationFor(message); + String messageValue = generateString(bundle.translationFor(message)); for (final Placeholder placeholder in message.placeholders) { if (placeholder.requiresFormatting) { messageValue = messageValue.replaceAll('{${placeholder.name}}', '\${${placeholder.name}String}'); @@ -164,7 +164,7 @@ String generateMethod(Message message, AppResourceBundle bundle) { } } - return generateString(messageValue, escapeDollar: false); + return messageValue; } if (message.isPlural) { 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 42289fd514..71ee04528d 100644 --- a/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart +++ b/packages/flutter_tools/test/integration.shard/gen_l10n_test.dart @@ -124,8 +124,12 @@ void main() { '#l10n 45 (Hello World of 101 citizens)\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 48 (\$!)\n' + '#l10n 49 (One \$)\n' + '#l10n 50 (Flutter\'s amazing!)\n' + '#l10n 51 (Flutter\'s amazing, times 2!)\n' + '#l10n 52 (Flutter is "amazing"!)\n' + '#l10n 53 (Flutter is "amazing", times 2!)\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 9313ff20ff..b20a575bf2 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 @@ -184,8 +184,12 @@ class Home extends StatelessWidget { '${localizations.helloWorldPopulation(1, 101)}', '${localizations.helloWorldPopulation(2, 102)}', '${localizations.helloWorldsInterpolation(123, "Hello", "World")}', + '${localizations.dollarSign}', + '${localizations.dollarSignPlural(1)}', '${localizations.singleQuote}', + '${localizations.singleQuotePlural(2)}', '${localizations.doubleQuote}', + '${localizations.doubleQuotePlural(2)}', ]); }, ), @@ -379,14 +383,43 @@ void main() { } }, + "dollarSign": "$!", + "@dollarSign": { + "description": "A message with a dollar sign." + }, + + "dollarSignPlural": "{count,plural, =1{One $} other{Many $}}", + "@dollarSignPlural": { + "description": "A plural message with a dollar sign.", + "placeholders": { + "count": {} + } + }, + "singleQuote": "Flutter's amazing!", "@singleQuote": { "description": "A message with a single quote." }, + "singleQuotePlural": "{count,plural, =1{Flutter's amazing, times 1!} other{Flutter's amazing, times {count}!}}", + "@singleQuotePlural": { + "description": "A plural message with a single quote.", + "placeholders": { + "count": {} + } + }, + "doubleQuote": "Flutter is \"amazing\"!", "@doubleQuote": { "description": "A message with double quotes." + }, + + "doubleQuotePlural": "{count,plural, =1{Flutter is \"amazing\", times 1!} other{Flutter is \"amazing\", times {count}!}}", + "@doubleQuotePlural": { + "description": "A plural message with double quotes.", + "placeholders": { + "count": {} + } } } '''; @@ -426,8 +459,12 @@ void main() { "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}}", "helloWorldInterpolation": "[{hello}] #{world}#", "helloWorldsInterpolation": "{count,plural, other {[{hello}] -{world}- #{count}#}}", + "dollarSign": "$!", + "dollarSignPlural": "{count,plural, =1{One $} other{Many $}}", "singleQuote": "Flutter's amazing!", - "doubleQuote": "Flutter is \"amazing\"!" + "singleQuotePlural": "{count,plural, =1{Flutter's amazing, times 1!} other{Flutter's amazing, times {count}!}", + "doubleQuote": "Flutter is \"amazing\"!", + "doubleQuotePlural": "{count,plural, =1{Flutter is \"amazing\", times 1!} other{Flutter is \"amazing\", times {count}!" } ''';