[gen_l10n] Handle single, double quotes, and dollar signs in strings (#54185)

This commit is contained in:
Per Classon 2020-04-07 21:21:02 +02:00 committed by GitHub
parent 08fe78fff9
commit e8d2907595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 7 deletions

View File

@ -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) {

View File

@ -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'
);
});

View File

@ -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}!"
}
''';