From 8be335f24c9d5212876a900876d8e10bfcab7737 Mon Sep 17 00:00:00 2001 From: Tae Hyung Kim Date: Tue, 25 Apr 2023 15:52:24 -0700 Subject: [PATCH] Handle dollar signs properly when generating localizations (#125514) Currently, the code doesn't properly handle strings which contain dollar signs. The return expression for the generated localization function is computed by `generateReturnExpr` which concatenates several strings, which are either interpolated placeholders, interpolated function calls, or normal strings, but we didn't properly escape dollar signs before sending normal strings to `generateReturnExpr`. Fixes #125461. --- .../lib/src/localizations/gen_l10n.dart | 2 +- .../localizations/localizations_utils.dart | 23 ++++++++++++--- .../generate_localizations_test.dart | 28 +++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart index 5c3538cbc8..5e2f9f805d 100644 --- a/packages/flutter_tools/lib/src/localizations/gen_l10n.dart +++ b/packages/flutter_tools/lib/src/localizations/gen_l10n.dart @@ -1131,7 +1131,7 @@ class LocalizationsGenerator { case ST.message: final List expressions = node.children.map((Node node) { if (node.type == ST.string) { - return node.value!; + return generateString(node.value!); } return generateVariables(node); }).toList(); diff --git a/packages/flutter_tools/lib/src/localizations/localizations_utils.dart b/packages/flutter_tools/lib/src/localizations/localizations_utils.dart index 4cc9c8ddd0..08bd615f1e 100644 --- a/packages/flutter_tools/lib/src/localizations/localizations_utils.dart +++ b/packages/flutter_tools/lib/src/localizations/localizations_utils.dart @@ -291,10 +291,25 @@ String generateString(String value) { return value; } -/// Given a list of strings, placeholders, or helper function calls, concatenate -/// them into one expression to be returned. +/// Given a list of normal strings or interpolated variables, concatenate them +/// into a single dart string to be returned. An example of a normal string +/// would be "'Hello world!'" and an example of a interpolated variable would be +/// "'$placeholder'". /// -/// If `isSingleStringVar` is passed, then we want to convert "'$expr'" to "expr". +/// Each of the strings in [expressions] should be a raw string, which, if it +/// were to be added to a dart file, would be a properly formatted dart string +/// with escapes and/or interpolation. The purpose of this function is to +/// concatenate these dart strings into a single dart string which can be +/// returned in the generated localization files. +/// +/// The following rules describe the kinds of string expressions that can be +/// handled: +/// 1. If [expressions] is empty, return the empty string "''". +/// 2. If [expressions] has only one [String] which is an interpolated variable, +/// it is converted to the variable itself e.g. ["'$expr'"] -> "expr". +/// 3. If one string in [expressions] is an interpolation and the next begins +/// with an alphanumeric character, then the former interpolation should be +/// wrapped in braces e.g. ["'$expr1'", "'another'"] -> "'${expr1}another'". String generateReturnExpr(List expressions, { bool isSingleStringVar = false }) { if (expressions.isEmpty) { return "''"; @@ -304,7 +319,7 @@ String generateReturnExpr(List expressions, { bool isSingleStringVar = f } else { final String string = expressions.reversed.fold('', (String string, String expression) { if (expression[0] != r'$') { - return generateString(expression) + string; + return expression + string; } final RegExp alphanumeric = RegExp(r'^([0-9a-zA-Z]|_)+$'); if (alphanumeric.hasMatch(expression.substring(1)) && !(string.isNotEmpty && alphanumeric.hasMatch(string[0]))) { 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 0ed3498d7b..2d9c014f4e 100644 --- a/packages/flutter_tools/test/general.shard/generate_localizations_test.dart +++ b/packages/flutter_tools/test/general.shard/generate_localizations_test.dart @@ -3244,4 +3244,32 @@ NumberFormat.decimalPatternDigits( ); ''')); }); + + // Regression test for https://github.com/flutter/flutter/issues/125461. + testWithoutContext('dollar signs are escaped properly when there is a select clause', () { + const String dollarSignWithSelect = r''' +{ + "dollarSignWithSelect": "$nice_bug\nHello Bug! Manifistation #1 {selectPlaceholder, select, case{message} other{messageOther}}" +}'''; + final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n') + ..createSync(recursive: true); + l10nDirectory.childFile(defaultTemplateArbFileName) + .writeAsStringSync(dollarSignWithSelect); + LocalizationsGenerator( + fileSystem: fs, + inputPathString: defaultL10nPathString, + outputPathString: defaultL10nPathString, + templateArbFileName: defaultTemplateArbFileName, + outputFileString: defaultOutputFileString, + classNameString: defaultClassNameString, + logger: logger, + suppressWarnings: true, + ) + ..loadResources() + ..writeOutputFiles(); + final String localizationsFile = fs.file( + fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'), + ).readAsStringSync(); + expect(localizationsFile, contains(r'\$nice_bug\nHello Bug! Manifistation #1 $_temp0')); + }); }