Unverified Commit 4996f60b authored by Shi-Hao Hong's avatar Shi-Hao Hong Committed by GitHub

[gen_l10n] Fix unintended use of raw string in generateString (#69382)

* Fix failing tests from introducing raw string generation in gen_l10n tool
parent c0ef9478
...@@ -378,9 +378,9 @@ class $classNamePrefix$camelCaseName extends $superClass {'''; ...@@ -378,9 +378,9 @@ class $classNamePrefix$camelCaseName extends $superClass {''';
/// ///
/// This function is used by tools that take in a JSON-formatted file to /// This function is used by tools that take in a JSON-formatted file to
/// generate Dart code. For this reason, characters with special meaning /// generate Dart code. For this reason, characters with special meaning
/// in JSON files. For example, the backspace character (\b) have to be /// in JSON files are escaped. For example, the backspace character (\b)
/// properly escaped by this function so that the generated Dart code /// has to be properly escaped by this function so that the generated
/// correctly represents this character: /// Dart code correctly represents this character:
/// ``` /// ```
/// foo\bar => 'foo\\bar' /// foo\bar => 'foo\\bar'
/// foo\nbar => 'foo\\nbar' /// foo\nbar => 'foo\\nbar'
......
...@@ -249,9 +249,9 @@ String describeLocale(String tag) { ...@@ -249,9 +249,9 @@ String describeLocale(String tag) {
/// ///
/// This function is used by tools that take in a JSON-formatted file to /// This function is used by tools that take in a JSON-formatted file to
/// generate Dart code. For this reason, characters with special meaning /// generate Dart code. For this reason, characters with special meaning
/// in JSON files. For example, the backspace character (\b) have to be /// in JSON files are escaped. For example, the backspace character (\b)
/// properly escaped by this function so that the generated Dart code /// has to be properly escaped by this function so that the generated
/// correctly represents this character: /// Dart code correctly represents this character:
/// ``` /// ```
/// foo\bar => 'foo\\bar' /// foo\bar => 'foo\\bar'
/// foo\nbar => 'foo\\nbar' /// foo\nbar => 'foo\\nbar'
...@@ -261,19 +261,6 @@ String describeLocale(String tag) { ...@@ -261,19 +261,6 @@ String describeLocale(String tag) {
/// foo$bar = 'foo\$bar' /// foo$bar = 'foo\$bar'
/// ``` /// ```
String generateString(String value) { String generateString(String value) {
if (<String>['\n', '\f', '\t', '\r', '\b'].every((String pattern) => !value.contains(pattern))) {
final bool hasDollar = value.contains(r'$');
final bool hasBackslash = value.contains(r'\');
final bool hasQuote = value.contains("'");
final bool hasDoubleQuote = value.contains('"');
if (!hasQuote) {
return hasBackslash || hasDollar ? "r'$value'" : "'$value'";
}
if (!hasDoubleQuote) {
return hasBackslash || hasDollar ? 'r"$value"' : '"$value"';
}
}
const String backslash = '__BACKSLASH__'; const String backslash = '__BACKSLASH__';
assert( assert(
!value.contains(backslash), !value.contains(backslash),
......
...@@ -1199,7 +1199,7 @@ void main() { ...@@ -1199,7 +1199,7 @@ void main() {
expect(baseLocalizationsFileContents, contains(r''' expect(baseLocalizationsFileContents, contains(r'''
/// In en, this message translates to: /// In en, this message translates to:
/// **'The price of this item is: \${price}'**''')); /// **'The price of this item is: \${price}'**'''));
}, skip: true); });
test('should generate a file per language', () { test('should generate a file per language', () {
const String singleEnCaMessageArbFileString = ''' const String singleEnCaMessageArbFileString = '''
......
...@@ -72,77 +72,77 @@ void main() { ...@@ -72,77 +72,77 @@ void main() {
'#l10n 19 (你好世界)\n' '#l10n 19 (你好世界)\n'
'#l10n 20 (你好2个其他世界)\n' '#l10n 20 (你好2个其他世界)\n'
'#l10n 21 (Hello 世界)\n' '#l10n 21 (Hello 世界)\n'
'#l10n 22 (--- scriptCode: zh_Hans ---)\n' '#l10n 22 (zh - Hello for 价钱 CNY123.00)\n'
'#l10n 23 (简体你好世界)\n' '#l10n 23 (--- scriptCode: zh_Hans ---)\n'
'#l10n 24 (--- scriptCode - zh_Hant ---)\n' '#l10n 24 (简体你好世界)\n'
'#l10n 25 (繁體你好世界)\n' '#l10n 25 (--- scriptCode - zh_Hant ---)\n'
'#l10n 26 (--- scriptCode - zh_Hant_TW ---)\n' '#l10n 26 (繁體你好世界)\n'
'#l10n 27 (台灣繁體你好世界)\n' '#l10n 27 (--- scriptCode - zh_Hant_TW ---)\n'
'#l10n 28 (--- General formatting tests ---)\n' '#l10n 28 (台灣繁體你好世界)\n'
'#l10n 29 (Hello World)\n' '#l10n 29 (--- General formatting tests ---)\n'
'#l10n 30 (Hello _NEWLINE_ World)\n' '#l10n 30 (Hello World)\n'
'#l10n 31 (Hello World)\n' '#l10n 31 (Hello _NEWLINE_ World)\n'
'#l10n 32 (Hello World)\n' '#l10n 32 (Hello \$ World)\n'
'#l10n 33 (Hello World on Friday, January 1, 1960)\n' '#l10n 33 (Hello World)\n'
'#l10n 34 (Hello world argument on 1/1/1960 at 00:00)\n' '#l10n 34 (Hello World)\n'
'#l10n 35 (Hello World from 1960 to 2020)\n' '#l10n 35 (Hello World on Friday, January 1, 1960)\n'
'#l10n 36 (Hello for 123)\n' '#l10n 36 (Hello world argument on 1/1/1960 at 00:00)\n'
'#l10n 37 (Hello for price USD123.00)\n' '#l10n 37 (Hello World from 1960 to 2020)\n'
'#l10n 38 (Hello)\n' '#l10n 38 (Hello for 123)\n'
'#l10n 39 (Hello World)\n' '#l10n 39 (Hello for price USD123.00)\n'
'#l10n 40 (Hello two worlds)\n' '#l10n 40 (Hello)\n'
'#l10n 41 (Hello)\n' '#l10n 41 (Hello World)\n'
'#l10n 42 (Hello new World)\n' '#l10n 42 (Hello two worlds)\n'
'#l10n 43 (Hello two new worlds)\n' '#l10n 43 (Hello)\n'
'#l10n 44 (Hello on Friday, January 1, 1960)\n' '#l10n 44 (Hello new World)\n'
'#l10n 45 (Hello World, on Friday, January 1, 1960)\n' '#l10n 45 (Hello two new worlds)\n'
'#l10n 46 (Hello two worlds, on Friday, January 1, 1960)\n' '#l10n 46 (Hello on Friday, January 1, 1960)\n'
'#l10n 47 (Hello other 0 worlds, with a total of 100 citizens)\n' '#l10n 47 (Hello World, on Friday, January 1, 1960)\n'
'#l10n 48 (Hello World of 101 citizens)\n' '#l10n 48 (Hello two worlds, on Friday, January 1, 1960)\n'
'#l10n 49 (Hello two worlds with 102 total citizens)\n' '#l10n 49 (Hello other 0 worlds, with a total of 100 citizens)\n'
'#l10n 50 ([Hello] -World- #123#)\n' '#l10n 50 (Hello World of 101 citizens)\n'
'#l10n 51 (\$!)\n' '#l10n 51 (Hello two worlds with 102 total citizens)\n'
'#l10n 52 (One \$)\n' '#l10n 52 ([Hello] -World- #123#)\n'
'#l10n 53 (Flutter\'s amazing!)\n' '#l10n 53 (\$!)\n'
'#l10n 54 (Flutter\'s amazing, times 2!)\n' '#l10n 54 (One \$)\n'
'#l10n 55 (Flutter is "amazing"!)\n' '#l10n 55 (Flutter\'s amazing!)\n'
'#l10n 56 (Flutter is "amazing", times 2!)\n' '#l10n 56 (Flutter\'s amazing, times 2!)\n'
'#l10n 57 (--- es ---)\n' '#l10n 57 (Flutter is "amazing"!)\n'
'#l10n 58 (ES - Hello world)\n' '#l10n 58 (Flutter is "amazing", times 2!)\n'
'#l10n 59 (ES - Hello _NEWLINE_ World)\n' '#l10n 59 (--- es ---)\n'
'#l10n 60 (ES - Hello Mundo)\n' '#l10n 60 (ES - Hello world)\n'
'#l10n 61 (ES - Hola Mundo)\n' '#l10n 61 (ES - Hello _NEWLINE_ World)\n'
'#l10n 62 (ES - Hello World on viernes, 1 de enero de 1960)\n' '#l10n 62 (ES - Hola \$ Mundo)\n'
'#l10n 63 (ES - Hello world argument on 1/1/1960 at 0:00)\n' '#l10n 63 (ES - Hello Mundo)\n'
'#l10n 64 (ES - Hello World from 1960 to 2020)\n' '#l10n 64 (ES - Hola Mundo)\n'
'#l10n 65 (ES - Hello for 123)\n' '#l10n 65 (ES - Hello World on viernes, 1 de enero de 1960)\n'
// Note that the space between "123.00" and "EUR" is a '#l10n 66 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
// non-breaking space (U+00A0) and not a regular space(U+0020) '#l10n 67 (ES - Hello World from 1960 to 2020)\n'
'#l10n 66 (ES - Hello for el precio 123,00 EUR)\n' '#l10n 68 (ES - Hello for 123)\n'
'#l10n 67 (ES - Hello)\n' '#l10n 69 (ES - Hello)\n'
'#l10n 68 (ES - Hello World)\n' '#l10n 70 (ES - Hello World)\n'
'#l10n 69 (ES - Hello two worlds)\n' '#l10n 71 (ES - Hello two worlds)\n'
'#l10n 70 (ES - Hello)\n' '#l10n 72 (ES - Hello)\n'
'#l10n 71 (ES - Hello nuevo World)\n' '#l10n 73 (ES - Hello nuevo World)\n'
'#l10n 72 (ES - Hello two nuevo worlds)\n' '#l10n 74 (ES - Hello two nuevo worlds)\n'
'#l10n 73 (ES - Hello on viernes, 1 de enero de 1960)\n' '#l10n 75 (ES - Hello on viernes, 1 de enero de 1960)\n'
'#l10n 74 (ES - Hello World, on viernes, 1 de enero de 1960)\n' '#l10n 76 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
'#l10n 75 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n' '#l10n 77 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
'#l10n 76 (ES - Hello other 0 worlds, with a total of 100 citizens)\n' '#l10n 78 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
'#l10n 77 (ES - Hello World of 101 citizens)\n' '#l10n 79 (ES - Hello World of 101 citizens)\n'
'#l10n 78 (ES - Hello two worlds with 102 total citizens)\n' '#l10n 80 (ES - Hello two worlds with 102 total citizens)\n'
'#l10n 79 (ES - [Hola] -Mundo- #123#)\n' '#l10n 81 (ES - [Hola] -Mundo- #123#)\n'
'#l10n 80 (ES - \$!)\n' '#l10n 82 (ES - \$!)\n'
'#l10n 81 (ES - One \$)\n' '#l10n 83 (ES - One \$)\n'
'#l10n 82 (ES - Flutter\'s amazing!)\n' '#l10n 84 (ES - Flutter\'s amazing!)\n'
'#l10n 83 (ES - Flutter\'s amazing, times 2!)\n' '#l10n 85 (ES - Flutter\'s amazing, times 2!)\n'
'#l10n 84 (ES - Flutter is "amazing"!)\n' '#l10n 86 (ES - Flutter is "amazing"!)\n'
'#l10n 85 (ES - Flutter is "amazing", times 2!)\n' '#l10n 87 (ES - Flutter is "amazing", times 2!)\n'
'#l10n 86 (--- es_419 ---)\n' '#l10n 88 (--- es_419 ---)\n'
'#l10n 87 (ES 419 - Hello World)\n'
'#l10n 88 (ES 419 - Hello)\n'
'#l10n 89 (ES 419 - Hello World)\n' '#l10n 89 (ES 419 - Hello World)\n'
'#l10n 90 (ES 419 - Hello two worlds)\n' '#l10n 90 (ES 419 - Hello)\n'
'#l10n 91 (ES 419 - Hello World)\n'
'#l10n 92 (ES 419 - Hello two worlds)\n'
'#l10n END\n' '#l10n END\n'
); );
} }
......
...@@ -137,6 +137,10 @@ class Home extends StatelessWidget { ...@@ -137,6 +137,10 @@ class Home extends StatelessWidget {
// Should use the fallback language, in this case, // Should use the fallback language, in this case,
// "Hello 世界" should be displayed. // "Hello 世界" should be displayed.
results.add(AppLocalizations.of(context).hello("世界")); results.add(AppLocalizations.of(context).hello("世界"));
// helloCost is tested in 'zh' because 'es' currency format contains a
// non-breaking space character (U+00A0), which if removed,
// makes it hard to decipher why the test is failing.
results.add(AppLocalizations.of(context).helloCost("价钱", 123));
}, },
), ),
LocaleBuilder( LocaleBuilder(
...@@ -172,6 +176,7 @@ class Home extends StatelessWidget { ...@@ -172,6 +176,7 @@ class Home extends StatelessWidget {
results.addAll(<String>[ results.addAll(<String>[
'${localizations.helloWorld}', '${localizations.helloWorld}',
'${localizations.helloNewlineWorld}', '${localizations.helloNewlineWorld}',
'${localizations.testDollarSign}',
'${localizations.hello("World")}', '${localizations.hello("World")}',
'${localizations.greeting("Hello", "World")}', '${localizations.greeting("Hello", "World")}',
'${localizations.helloWorldOn(DateTime(1960))}', '${localizations.helloWorldOn(DateTime(1960))}',
...@@ -210,13 +215,16 @@ class Home extends StatelessWidget { ...@@ -210,13 +215,16 @@ class Home extends StatelessWidget {
results.addAll(<String>[ results.addAll(<String>[
'${localizations.helloWorld}', '${localizations.helloWorld}',
'${localizations.helloNewlineWorld}', '${localizations.helloNewlineWorld}',
'${localizations.testDollarSign}',
'${localizations.hello("Mundo")}', '${localizations.hello("Mundo")}',
'${localizations.greeting("Hola", "Mundo")}', '${localizations.greeting("Hola", "Mundo")}',
'${localizations.helloWorldOn(DateTime(1960))}', '${localizations.helloWorldOn(DateTime(1960))}',
'${localizations.helloOn("world argument", DateTime(1960), DateTime(1960))}', '${localizations.helloOn("world argument", DateTime(1960), DateTime(1960))}',
'${localizations.helloWorldDuring(DateTime(1960), DateTime(2020))}', '${localizations.helloWorldDuring(DateTime(1960), DateTime(2020))}',
'${localizations.helloFor(123)}', '${localizations.helloFor(123)}',
'${localizations.helloCost("el precio", 123)}', // helloCost is tested in 'zh' because 'es' currency format contains a
// non-breaking space character (U+00A0), which if removed,
// makes it hard to decipher why the test is failing.
'${localizations.helloWorlds(0)}', '${localizations.helloWorlds(0)}',
'${localizations.helloWorlds(1)}', '${localizations.helloWorlds(1)}',
'${localizations.helloWorlds(2)}', '${localizations.helloWorlds(2)}',
...@@ -299,6 +307,11 @@ void main() { ...@@ -299,6 +307,11 @@ void main() {
"description": "The JSON decoder should convert backslash-n to a newline character in the generated Dart string." "description": "The JSON decoder should convert backslash-n to a newline character in the generated Dart string."
}, },
"testDollarSign": "Hello $ World",
"@testDollarSign": {
"description": "The generated Dart String should handle the dollar sign correctly."
},
"hello": "Hello {world}", "hello": "Hello {world}",
"@hello": { "@hello": {
"description": "A message with a single parameter", "description": "A message with a single parameter",
...@@ -507,13 +520,13 @@ void main() { ...@@ -507,13 +520,13 @@ void main() {
"helloWorld": "ES - Hello world", "helloWorld": "ES - Hello world",
"helloWorlds": "{count,plural, =0{ES - Hello} =1{ES - Hello World} =2{ES - Hello two worlds} few{ES - Hello {count} worlds} many{ES - Hello all {count} worlds} other{ES - Hello other {count} worlds}}", "helloWorlds": "{count,plural, =0{ES - Hello} =1{ES - Hello World} =2{ES - Hello two worlds} few{ES - Hello {count} worlds} many{ES - Hello all {count} worlds} other{ES - Hello other {count} worlds}}",
"helloNewlineWorld": "ES - Hello \n World", "helloNewlineWorld": "ES - Hello \n World",
"testDollarSign": "ES - Hola $ Mundo",
"hello": "ES - Hello {world}", "hello": "ES - Hello {world}",
"greeting": "ES - {hello} {world}", "greeting": "ES - {hello} {world}",
"helloWorldOn": "ES - Hello World on {date}", "helloWorldOn": "ES - Hello World on {date}",
"helloWorldDuring": "ES - Hello World from {startDate} to {endDate}", "helloWorldDuring": "ES - Hello World from {startDate} to {endDate}",
"helloOn": "ES - Hello {world} on {date} at {time}", "helloOn": "ES - Hello {world} on {date} at {time}",
"helloFor": "ES - Hello for {value}", "helloFor": "ES - Hello for {value}",
"helloCost": "ES - Hello for {price} {value}",
"helloAdjectiveWorlds": "{count,plural, =0{ES - Hello} =1{ES - Hello {adjective} World} =2{ES - Hello two {adjective} worlds} other{ES - Hello other {count} {adjective} worlds}}", "helloAdjectiveWorlds": "{count,plural, =0{ES - Hello} =1{ES - Hello {adjective} World} =2{ES - Hello two {adjective} worlds} other{ES - Hello other {count} {adjective} worlds}}",
"helloWorldsOn": "{count,plural, =0{ES - Hello on {date}} =1{ES - Hello World, on {date}} =2{ES - Hello two worlds, on {date}} other{ES - Hello other {count} worlds, on {date}}}", "helloWorldsOn": "{count,plural, =0{ES - Hello on {date}} =1{ES - Hello World, on {date}} =2{ES - Hello two worlds, on {date}} other{ES - Hello other {count} worlds, on {date}}}",
"helloWorldPopulation": "{ES - count,plural, =1{ES - Hello World of {population} citizens} =2{ES - Hello two worlds with {population} total citizens} many{ES - Hello all {count} worlds, with a total of {population} citizens} other{ES - Hello other {count} worlds, with a total of {population} citizens}}", "helloWorldPopulation": "{ES - count,plural, =1{ES - Hello World of {population} citizens} =2{ES - Hello two worlds with {population} total citizens} many{ES - Hello all {count} worlds, with a total of {population} citizens} other{ES - Hello other {count} worlds, with a total of {population} citizens}}",
...@@ -540,7 +553,8 @@ void main() { ...@@ -540,7 +553,8 @@ void main() {
{ {
"@@locale": "zh", "@@locale": "zh",
"helloWorld": "你好世界", "helloWorld": "你好世界",
"helloWorlds": "{count,plural, =0{你好} =1{你好世界} other{你好{count}个其他世界}}" "helloWorlds": "{count,plural, =0{你好} =1{你好世界} other{你好{count}个其他世界}}",
"helloCost": "zh - Hello for {price} {value}"
} }
'''; ''';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment