[gen_l10n] correct variable name when the placeholder requiresFormatting (#86842)
diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart
index 85dab53..014d1f3 100644
--- a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart
+++ b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart
@@ -202,21 +202,16 @@
if (match != null && match.groupCount == 2) {
String argValue = generateString(match.group(2)!);
for (final Placeholder placeholder in message.placeholders) {
- if (placeholder != countPlaceholder && placeholder.requiresFormatting) {
- argValue = argValue.replaceAll(
- '#${placeholder.name}#',
- _needsCurlyBracketStringInterpolation(argValue, placeholder.name)
- ? '\${${placeholder.name}String}'
- : '\$${placeholder.name}String'
- );
- } else {
- argValue = argValue.replaceAll(
- '#${placeholder.name}#',
- _needsCurlyBracketStringInterpolation(argValue, placeholder.name)
- ? '\${${placeholder.name}}'
- : '\$${placeholder.name}'
- );
+ String variable = placeholder.name;
+ if (placeholder.requiresFormatting) {
+ variable += 'String';
}
+ argValue = argValue.replaceAll(
+ '#${placeholder.name}#',
+ _needsCurlyBracketStringInterpolation(argValue, placeholder.name)
+ ? '\${$variable}'
+ : '\$$variable'
+ );
}
pluralLogicArgs.add(' ${pluralIds[pluralKey]}: $argValue');
}
@@ -368,21 +363,16 @@
String generateMessage() {
String messageValue = generateString(translationForMessage);
for (final Placeholder placeholder in message.placeholders) {
+ String variable = placeholder.name;
if (placeholder.requiresFormatting) {
- messageValue = messageValue.replaceAll(
- '{${placeholder.name}}',
- _needsCurlyBracketStringInterpolation(messageValue, placeholder.name)
- ? '\${${placeholder.name}String}'
- : '\$${placeholder.name}String'
- );
- } else {
- messageValue = messageValue.replaceAll(
- '{${placeholder.name}}',
- _needsCurlyBracketStringInterpolation(messageValue, placeholder.name)
- ? '\${${placeholder.name}}'
- : '\$${placeholder.name}'
- );
+ variable += 'String';
}
+ messageValue = messageValue.replaceAll(
+ '{${placeholder.name}}',
+ _needsCurlyBracketStringInterpolation(messageValue, placeholder.name)
+ ? '\${$variable}'
+ : '\$$variable'
+ );
}
return messageValue;
diff --git a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart
index fcaa2ac..ba1f042 100644
--- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart
+++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart
@@ -2166,6 +2166,16 @@
"placeholders": {
"count": {}
}
+ },
+ "third": "{total,plural, =0{test {total}} other{ {total}}",
+ "@third": {
+ "description": "Third set of plural messages to test, for number.",
+ "placeholders": {
+ "total": {
+ "type": "int",
+ "format": "compactLong"
+ }
+ }
}
}
''';
@@ -2206,6 +2216,9 @@
expect(localizationsFile, contains(r'${count}m'));
expect(localizationsFile, contains(r'test $count'));
expect(localizationsFile, contains(r' $count'));
+ expect(localizationsFile, contains(r'String totalString = totalNumberFormat'));
+ expect(localizationsFile, contains(r'test $totalString'));
+ expect(localizationsFile, contains(r' $totalString'));
});
testWithoutContext(
@@ -2493,7 +2506,6 @@
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file.dart'),
).readAsStringSync();
- print(localizationsFile);
expect(localizationsFile, containsIgnoringWhitespace(r'''
AppLocalizations lookupAppLocalizations(Locale locale) {
'''));