[gen_l10n] Improvements to gen_l10n (#116202)

* init

* fix tests

* fix lint

* extra changes

* oops missed some merge conflicts

* fix lexer add tests

* consistent warnings and errors

* throw error at the end

* improve efficiency, improve code generation

* fix

* nit

* fix test

* remove helper method class

* two d's

* oops

* empty commit as google testing won't pass :(
This commit is contained in:
Tae Hyung Kim
2022-11-30 15:07:06 -08:00
committed by GitHub
parent 43d5cbb15e
commit 7802c7acd8
8 changed files with 433 additions and 583 deletions

View File

@@ -1284,6 +1284,40 @@ class AppLocalizationsEn extends AppLocalizations {
});
group('writeOutputFiles', () {
testWithoutContext('multiple messages with syntax error all log their errors', () {
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(r'''
{
"msg1": "{",
"msg2": "{ {"
}''');
l10nDirectory.childFile(esArbFileName)
.writeAsStringSync(singleEsMessageArbFileString);
try {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
} on L10nException catch (error) {
expect(error.message, equals('Found syntax errors.'));
expect(logger.errorText, contains('''
[app_en.arb:msg1] ICU Syntax Error: Expected "identifier" but found no tokens.
{
^
[app_en.arb:msg2] ICU Syntax Error: Expected "identifier" but found "{".
{ {
^'''));
}
});
testWithoutContext('message without placeholders - should generate code comment with description and template message translation', () {
_standardFlutterDirectoryL10nSetup(fs);
LocalizationsGenerator(
@@ -1581,47 +1615,31 @@ import 'output-localization-file_en.dart' deferred as output-localization-file_e
});
group('placeholder tests', () {
testWithoutContext('should throw attempting to generate a select message without placeholders', () {
const String selectMessageWithoutPlaceholdersAttribute = '''
testWithoutContext('should automatically infer placeholders that are not explicitly defined', () {
const String messageWithoutDefinedPlaceholder = '''
{
"helloWorld": "Hello {name}",
"@helloWorld": {
"description": "Improperly formatted since it has no placeholder attribute.",
"placeholders": {
"hello": {},
"world": {}
}
}
"helloWorld": "Hello {name}"
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(selectMessageWithoutPlaceholdersAttribute);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified placeholder is defined in your arb file.
[app_en.arb:helloWorld] Hello {name}
^'''),
)),
);
.writeAsStringSync(messageWithoutDefinedPlaceholder);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
).readAsStringSync();
expect(localizationsFile, contains('String helloWorld(Object name) {'));
});
});
@@ -1909,7 +1927,37 @@ Make sure that the specified placeholder is defined in your arb file.
});
group('plural messages', () {
testWithoutContext('should throw attempting to generate a plural message without placeholders', () {
testWithoutContext('warnings are generated when plural parts are repeated', () {
const String pluralMessageWithOverriddenParts = '''
{
"helloWorlds": "{count,plural, =0{Hello}zero{hello} other{hi}}",
"@helloWorlds": {
"description": "Properly formatted but has redundant zero cases."
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithOverriddenParts);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
expect(logger.hadWarningOutput, isTrue);
expect(logger.warningText, contains('''
[app_en.arb:helloWorlds] ICU Syntax Warning: The plural part specified below is overridden by a later plural part.
{count,plural, =0{Hello}zero{hello} other{hi}}
^'''));
});
testWithoutContext('should automatically infer plural placeholders that are not explicitly defined', () {
const String pluralMessageWithoutPlaceholdersAttribute = '''
{
"helloWorlds": "{count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}",
@@ -1922,106 +1970,21 @@ Make sure that the specified placeholder is defined in your arb file.
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithoutPlaceholdersAttribute);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified plural placeholder is defined in your arb file.
[app_en.arb:helloWorlds] {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}
^'''),
)),
);
});
testWithoutContext('should throw attempting to generate a plural message with an empty placeholders map', () {
const String pluralMessageWithEmptyPlaceholdersMap = '''
{
"helloWorlds": "{count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}",
"@helloWorlds": {
"description": "Improperly formatted since it has no placeholder attribute.",
"placeholders": {}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithEmptyPlaceholdersMap);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified plural placeholder is defined in your arb file.
[app_en.arb:helloWorlds] {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}
^'''),
)),
);
});
testWithoutContext('should throw attempting to generate a plural message with no resource attributes', () {
const String pluralMessageWithoutResourceAttributes = '''
{
"helloWorlds": "{count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}"
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithoutResourceAttributes);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified plural placeholder is defined in your arb file.
[app_en.arb:helloWorlds] {count,plural, =0{Hello}=1{Hello World}=2{Hello two worlds}few{Hello {count} worlds}many{Hello all {count} worlds}other{Hello other {count} worlds}}
^'''),
)),
);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
).readAsStringSync();
expect(localizationsFile, contains('String helloWorlds(num count) {'));
});
testWithoutContext('should throw attempting to generate a plural message with incorrect format for placeholders', () {
@@ -2065,7 +2028,7 @@ Make sure that the specified plural placeholder is defined in your arb file.
});
group('select messages', () {
testWithoutContext('should throw attempting to generate a select message without placeholders', () {
testWithoutContext('should auotmatically infer select placeholders that are not explicitly defined', () {
const String selectMessageWithoutPlaceholdersAttribute = '''
{
"genderSelect": "{gender, select, female {She} male {He} other {they} }",
@@ -2078,106 +2041,21 @@ Make sure that the specified plural placeholder is defined in your arb file.
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(selectMessageWithoutPlaceholdersAttribute);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified select placeholder is defined in your arb file.
[app_en.arb:genderSelect] {gender, select, female {She} male {He} other {they} }
^'''),
)),
);
});
testWithoutContext('should throw attempting to generate a select message with an empty placeholders map', () {
const String selectMessageWithEmptyPlaceholdersMap = '''
{
"genderSelect": "{gender, select, female {She} male {He} other {they} }",
"@genderSelect": {
"description": "Improperly formatted since it has no placeholder attribute.",
"placeholders": {}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(selectMessageWithEmptyPlaceholdersMap);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified select placeholder is defined in your arb file.
[app_en.arb:genderSelect] {gender, select, female {She} male {He} other {they} }
^'''),
)),
);
});
testWithoutContext('should throw attempting to generate a select message with no resource attributes', () {
const String selectMessageWithoutResourceAttributes = '''
{
"genderSelect": "{gender, select, female {She} male {He} other {they} }"
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(selectMessageWithoutResourceAttributes);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Make sure that the specified select placeholder is defined in your arb file.
[app_en.arb:genderSelect] {gender, select, female {She} male {He} other {they} }
^'''),
)),
);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
).readAsStringSync();
expect(localizationsFile, contains('String genderSelect(String gender) {'));
});
testWithoutContext('should throw attempting to generate a select message with incorrect format for placeholders', () {
@@ -2234,30 +2112,25 @@ Make sure that the specified select placeholder is defined in your arb file.
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(selectMessageWithoutPlaceholdersAttribute);
expect(
() {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
},
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
contains('''
Select expressions must have an "other" case.
[app_en.arb:genderSelect] {gender, select,}
^'''),
)),
);
try {
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
)
..loadResources()
..writeOutputFiles();
} on L10nException {
expect(logger.errorText, contains('''
[app_en.arb:genderSelect] ICU Syntax Error: Select expressions must have an "other" case.
{gender, select,}
^''')
);
}
});
});
@@ -2984,37 +2857,66 @@ AppLocalizations lookupAppLocalizations(Locale locale) {
'''));
});
// TODO(thkim1011): Uncomment when implementing escaping.
// See https://github.com/flutter/flutter/issues/113455.
// testWithoutContext('escaping with single quotes', () {
// const String arbFile = '''
// {
// "singleQuote": "Flutter''s amazing!",
// "@singleQuote": {
// "description": "A message with a single quote."
// }
// }''';
testWithoutContext('escaping with single quotes', () {
const String arbFile = '''
{
"singleQuote": "Flutter''s amazing!",
"@singleQuote": {
"description": "A message with a single quote."
}
}''';
// final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
// ..createSync(recursive: true);
// l10nDirectory.childFile(defaultTemplateArbFileName)
// .writeAsStringSync(arbFile);
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(arbFile);
// LocalizationsGenerator(
// fileSystem: fs,
// inputPathString: defaultL10nPathString,
// outputPathString: defaultL10nPathString,
// templateArbFileName: defaultTemplateArbFileName,
// outputFileString: defaultOutputFileString,
// classNameString: defaultClassNameString,
// logger: logger,
// )
// ..loadResources()
// ..writeOutputFiles();
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
useEscaping: true,
)
..loadResources()
..writeOutputFiles();
// final String localizationsFile = fs.file(
// fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
// ).readAsStringSync();
// expect(localizationsFile, contains(r"Flutter\'s amazing"));
// });
final String localizationsFile = fs.file(
fs.path.join(syntheticL10nPackagePath, 'output-localization-file_en.dart'),
).readAsStringSync();
expect(localizationsFile, contains(r"Flutter\'s amazing"));
});
testWithoutContext('suppress warnings flag actually suppresses warnings', () {
const String pluralMessageWithOverriddenParts = '''
{
"helloWorlds": "{count,plural, =0{Hello}zero{hello} other{hi}}",
"@helloWorlds": {
"description": "Properly formatted but has redundant zero cases.",
"placeholders": {
"count": {}
}
}
}''';
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
..createSync(recursive: true);
l10nDirectory.childFile(defaultTemplateArbFileName)
.writeAsStringSync(pluralMessageWithOverriddenParts);
LocalizationsGenerator(
fileSystem: fs,
inputPathString: defaultL10nPathString,
outputPathString: defaultL10nPathString,
templateArbFileName: defaultTemplateArbFileName,
outputFileString: defaultOutputFileString,
classNameString: defaultClassNameString,
logger: logger,
suppressWarnings: true,
)
..loadResources()
..writeOutputFiles();
expect(logger.hadWarningOutput, isFalse);
});
}

View File

@@ -218,6 +218,14 @@ void main() {
]));
});
testWithoutContext('lexer identifier names can be "select" or "plural"', () {
final List<Node> tokens = Parser('keywords', 'app_en.arb', '{ select } { plural, select, singular{test} other{hmm} }').lexIntoTokens();
expect(tokens[1].value, equals('select'));
expect(tokens[1].type, equals(ST.identifier));
expect(tokens[5].value, equals('plural'));
expect(tokens[5].type, equals(ST.identifier));
});
testWithoutContext('lexer: lexically correct but syntactically incorrect', () {
final List<Node> tokens = Parser(
'syntax',
@@ -242,9 +250,9 @@ void main() {
testWithoutContext('lexer unmatched single quote', () {
const String message = "here''s an unmatched single quote: '";
const String expectedError = '''
ICU Lexing Error: Unmatched single quotes.
[app_en.arb:escaping] here''s an unmatched single quote: '
^''';
[app_en.arb:escaping] ICU Lexing Error: Unmatched single quotes.
here''s an unmatched single quote: '
^''';
expect(
() => Parser('escaping', 'app_en.arb', message, useEscaping: true).lexIntoTokens(),
throwsA(isA<L10nException>().having(
@@ -257,9 +265,9 @@ ICU Lexing Error: Unmatched single quotes.
testWithoutContext('lexer unexpected character', () {
const String message = '{ * }';
const String expectedError = '''
ICU Lexing Error: Unexpected character.
[app_en.arb:lex] { * }
^''';
[app_en.arb:lex] ICU Lexing Error: Unexpected character.
{ * }
^''';
expect(
() => Parser('lex', 'app_en.arb', message).lexIntoTokens(),
throwsA(isA<L10nException>().having(
@@ -460,11 +468,11 @@ ICU Lexing Error: Unexpected character.
testWithoutContext('parser unexpected token', () {
// unexpected token
const String expectedError1 = '''
ICU Syntax Error: Expected "}" but found "=".
[app_en.arb:unexpectedToken] { placeholder =
^''';
[app_en.arb:unexpectedToken] ICU Syntax Error: Expected "}" but found "=".
{ placeholder =
^''';
expect(
() => Parser('unexpectedToken', 'app_en.arb', '{ placeholder =').parse(),
() => Parser('unexpectedToken', 'app_en.arb', '{ placeholder =').parseIntoTree(),
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
@@ -472,11 +480,11 @@ ICU Syntax Error: Expected "}" but found "=".
)));
const String expectedError2 = '''
ICU Syntax Error: Expected "number" but found "}".
[app_en.arb:unexpectedToken] { count, plural, = }
^''';
[app_en.arb:unexpectedToken] ICU Syntax Error: Expected "number" but found "}".
{ count, plural, = }
^''';
expect(
() => Parser('unexpectedToken', 'app_en.arb', '{ count, plural, = }').parse(),
() => Parser('unexpectedToken', 'app_en.arb', '{ count, plural, = }').parseIntoTree(),
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',
@@ -484,11 +492,11 @@ ICU Syntax Error: Expected "number" but found "}".
)));
const String expectedError3 = '''
ICU Syntax Error: Expected "identifier" but found ",".
[app_en.arb:unexpectedToken] { , plural , = }
^''';
[app_en.arb:unexpectedToken] ICU Syntax Error: Expected "identifier" but found ",".
{ , plural , = }
^''';
expect(
() => Parser('unexpectedToken', 'app_en.arb', '{ , plural , = }').parse(),
() => Parser('unexpectedToken', 'app_en.arb', '{ , plural , = }').parseIntoTree(),
throwsA(isA<L10nException>().having(
(L10nException e) => e.message,
'message',