gen_l10n Number Format Handling (#47706)
This commit is contained in:
committed by
Flutter GitHub Bot
parent
846dce92ff
commit
b038b40941
@@ -946,6 +946,97 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
test('correctly generates simple message with numbers', () {
|
||||
const String singleNumberMessage = '''{
|
||||
"courseCompletion": "You have completed {progress} of the course.",
|
||||
"@courseCompletion": {
|
||||
"description": "The amount of progress the student has made in their class.",
|
||||
"placeholders": {
|
||||
"progress": {
|
||||
"type": "Number",
|
||||
"format": "percentPattern"
|
||||
}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(singleNumberMessage);
|
||||
|
||||
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
|
||||
try {
|
||||
generator.initialize(
|
||||
l10nDirectoryPath: defaultArbPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
);
|
||||
generator.parseArbFiles();
|
||||
generator.generateClassMethods();
|
||||
} on Exception catch (e) {
|
||||
fail('Parsing template arb file should succeed: \n$e');
|
||||
}
|
||||
|
||||
expect(generator.classMethods, isNotEmpty);
|
||||
expect(
|
||||
generator.classMethods.first,
|
||||
''' String courseCompletion(Object progress) {
|
||||
final NumberFormat progressNumberFormat = NumberFormat.percentPattern(
|
||||
locale: _localeName,
|
||||
);
|
||||
final String progressString = progressNumberFormat.format(progress);
|
||||
|
||||
return Intl.message(
|
||||
r'You have completed \$progressString of the course.',
|
||||
locale: _localeName,
|
||||
name: 'courseCompletion',
|
||||
desc: r'The amount of progress the student has made in their class.',
|
||||
args: <Object>[progressString]
|
||||
);
|
||||
}
|
||||
''');
|
||||
});
|
||||
|
||||
test('throws an exception when improperly formatted number is passed in', () {
|
||||
const String singleDateMessageArbFileString = '''{
|
||||
"courseCompletion": "You have completed {progress} of the course.",
|
||||
"@courseCompletion": {
|
||||
"description": "The amount of progress the student has made in their class.",
|
||||
"placeholders": {
|
||||
"progress": {
|
||||
"type": "Number",
|
||||
"format": "asdf"
|
||||
}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(singleDateMessageArbFileString);
|
||||
|
||||
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
|
||||
try {
|
||||
generator.initialize(
|
||||
l10nDirectoryPath: defaultArbPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
);
|
||||
generator.parseArbFiles();
|
||||
generator.generateClassMethods();
|
||||
} on L10nException catch (e) {
|
||||
expect(e.message, contains('asdf'));
|
||||
expect(e.message, contains('progress'));
|
||||
expect(e.message, contains('does not have a corresponding NumberFormat'));
|
||||
return;
|
||||
}
|
||||
|
||||
fail('Improper date formatting should throw an exception');
|
||||
});
|
||||
|
||||
test('correctly generates a plural message with placeholders:', () {
|
||||
const String pluralMessageWithMultiplePlaceholders = '''{
|
||||
"helloWorlds": "{count,plural, =0{Hello}=1{Hello {adjective} World}=2{Hello two {adjective} worlds}few{Hello {count} {adjective} worlds}many{Hello all {count} {adjective} worlds}other{Hello other {count} {adjective} worlds}}",
|
||||
@@ -1050,6 +1141,118 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('correctly generates a plural message with number placeholders:', () {
|
||||
const String pluralMessageWithDateTimePlaceholder = '''{
|
||||
"helloWorlds": "{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}}",
|
||||
"@helloWorlds": {
|
||||
"placeholders": {
|
||||
"count": {},
|
||||
"population": {
|
||||
"type": "Number",
|
||||
"format": "compactLong"
|
||||
}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(pluralMessageWithDateTimePlaceholder);
|
||||
|
||||
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
|
||||
try {
|
||||
generator.initialize(
|
||||
l10nDirectoryPath: defaultArbPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
);
|
||||
generator.parseArbFiles();
|
||||
generator.generateClassMethods();
|
||||
} on Exception catch (e) {
|
||||
fail('Parsing template arb file should succeed: \n$e');
|
||||
}
|
||||
|
||||
expect(generator.classMethods, isNotEmpty);
|
||||
expect(
|
||||
generator.classMethods.first,
|
||||
''' String helloWorlds(int count, Object population) {
|
||||
final NumberFormat populationNumberFormat = NumberFormat.compactLong(
|
||||
locale: _localeName,
|
||||
);
|
||||
final String populationString = populationNumberFormat.format(population);
|
||||
|
||||
return Intl.plural(
|
||||
count,
|
||||
locale: _localeName,
|
||||
name: 'helloWorlds',
|
||||
args: <Object>[count, populationString],
|
||||
one: 'Hello World of \$populationString citizens',
|
||||
two: 'Hello two worlds with \$populationString total citizens',
|
||||
many: 'Hello all \$count worlds, with a total of \$populationString citizens',
|
||||
other: 'Hello other \$count worlds, with a total of \$populationString citizens'
|
||||
);
|
||||
}
|
||||
'''
|
||||
);
|
||||
});
|
||||
|
||||
test('correctly adds optional parameters to numbers:', () {
|
||||
const String singleNumberMessage = '''{
|
||||
"courseCompletion": "You have completed {progress} of the course.",
|
||||
"@courseCompletion": {
|
||||
"description": "The amount of progress the student has made in their class.",
|
||||
"placeholders": {
|
||||
"progress": {
|
||||
"type": "Number",
|
||||
"format": "decimalPercentPattern",
|
||||
"optionalParameters": {
|
||||
"decimalDigits": 2
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}''';
|
||||
final Directory l10nDirectory = fs.currentDirectory.childDirectory('lib').childDirectory('l10n')
|
||||
..createSync(recursive: true);
|
||||
l10nDirectory.childFile(defaultTemplateArbFileName)
|
||||
.writeAsStringSync(singleNumberMessage);
|
||||
|
||||
final LocalizationsGenerator generator = LocalizationsGenerator(fs);
|
||||
try {
|
||||
generator.initialize(
|
||||
l10nDirectoryPath: defaultArbPathString,
|
||||
templateArbFileName: defaultTemplateArbFileName,
|
||||
outputFileString: defaultOutputFileString,
|
||||
classNameString: defaultClassNameString,
|
||||
);
|
||||
generator.parseArbFiles();
|
||||
generator.generateClassMethods();
|
||||
} on Exception catch (e) {
|
||||
fail('Parsing template arb file should succeed: \n$e');
|
||||
}
|
||||
|
||||
expect(generator.classMethods, isNotEmpty);
|
||||
expect(
|
||||
generator.classMethods.first,
|
||||
''' String courseCompletion(Object progress) {
|
||||
final NumberFormat progressNumberFormat = NumberFormat.decimalPercentPattern(
|
||||
locale: _localeName,
|
||||
decimalDigits: 2,
|
||||
);
|
||||
final String progressString = progressNumberFormat.format(progress);
|
||||
|
||||
return Intl.message(
|
||||
r'You have completed \$progressString of the course.',
|
||||
locale: _localeName,
|
||||
name: 'courseCompletion',
|
||||
desc: r'The amount of progress the student has made in their class.',
|
||||
args: <Object>[progressString]
|
||||
);
|
||||
}
|
||||
''');
|
||||
});
|
||||
|
||||
test('should throw attempting to generate a plural message without placeholders:', () {
|
||||
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}}",
|
||||
|
||||
Reference in New Issue
Block a user