diff --git a/dev/tools/localization/gen_l10n.dart b/dev/tools/localization/gen_l10n.dart index c3dad4e111..0b990c3c6b 100644 --- a/dev/tools/localization/gen_l10n.dart +++ b/dev/tools/localization/gen_l10n.dart @@ -136,7 +136,7 @@ const String getterMethodTemplate = ''' '''; const String simpleMethodTemplate = ''' - String @methodName(@methodParameters) {@dateFormatting + String @methodName(@methodParameters) {@dateFormatting@numberFormatting return Intl.message( @message, locale: _localeName, @@ -146,7 +146,7 @@ const String simpleMethodTemplate = ''' '''; const String pluralMethodTemplate = ''' - String @methodName(@methodParameters) {@dateFormatting + String @methodName(@methodParameters) {@dateFormatting@numberFormatting return Intl.plural( @intlMethodArgs ); @@ -214,34 +214,70 @@ const Set allowableDateFormats = { 's', }; -bool _isDateParameter(Map placeholderValue) => placeholderValue['type'] == 'DateTime'; +// The set of number formats that can be automatically localized. +// +// The localizations generation tool makes use of the intl library's +// NumberFormat class to properly format numbers based on the locale, the +// desired format, as well as the passed in number. For example, using +// DateFormat.compactLong("en_US").format(1200000) results +// in the string "1.2 million". +// +// Since the tool generates code that uses NumberFormat's constructor, it is +// necessary to verify that the constructor exists, or the +// tool will generate code that may cause a compile-time error. +// +// See also: +// +// * +// * +const Set allowableNumberFormats = { + 'compact', + 'compactLong', + 'decimalPattern', + 'decimalPercentPattern', + 'percentPattern', + 'scientificPattern', +}; -bool _dateParameterIsValid(Map placeholderValue, String placeholder) { +bool _isDateParameter(Map placeholderValue) => placeholderValue['type'] == 'DateTime'; +bool _isNumberParameter(Map placeholderValue) => placeholderValue['type'] == 'Number'; +bool _containsFormatKey(Map placeholderValue, String placeholder) { + if (placeholderValue.containsKey('format')) + return true; + throw L10nException( + 'The placeholder, $placeholder, has its "type" resource attribute set to ' + 'the "${placeholderValue['type']}" type. To properly resolve for the right ' + '${placeholderValue['type']} format, the "format" attribute needs to be set ' + 'to determine which DateFormat to use. \n' + 'Check the intl library\'s DateFormat class constructors for allowed ' + 'date formats.' + ); +} + +bool _isValidDateParameter(Map placeholderValue, String placeholder) { if (allowableDateFormats.contains(placeholderValue['format'])) return true; throw L10nException( - 'Date format ${placeholderValue['format']} for the $placeholder \n' + 'Date format ${placeholderValue['format']} for $placeholder \n' 'placeholder does not have a corresponding DateFormat \n' 'constructor. Check the intl library\'s DateFormat class \n' 'constructors for allowed date formats.' ); } -bool _containsFormatKey(Map placeholderValue, String placeholder) { - if (placeholderValue.containsKey('format')) +bool _isValidNumberParameter(Map placeholderValue, String placeholder) { + if (allowableNumberFormats.contains(placeholderValue['format'])) return true; throw L10nException( - 'The placeholder, $placeholder, has its "type" resource attribute set to ' - 'the "DateTime" type. To properly resolve for the right DateTime format, ' - 'the "format" attribute needs to be set to determine which DateFormat to ' - 'use. \n' - 'Check the intl library\'s DateFormat class constructors for allowed ' - 'date formats.' + 'Number format ${placeholderValue['format']} for the $placeholder \n' + 'placeholder does not have a corresponding NumberFormat \n' + 'constructor. Check the intl library\'s NumberFormat class \n' + 'constructors for allowed number formats.' ); } -List genMethodParameters(Map bundle, String key, String type) { - final Map attributesMap = bundle['@$key'] as Map; +List genMethodParameters(Map bundle, String resourceId, String type) { + final Map attributesMap = bundle['@$resourceId'] as Map; if (attributesMap != null && attributesMap.containsKey('placeholders')) { final Map placeholders = attributesMap['placeholders'] as Map; return placeholders.keys.map((String parameter) => '$type $parameter').toList(); @@ -265,34 +301,78 @@ List genPluralMethodParameters(Iterable placeholderKeys, String }).toList(); } -String generateDateFormattingLogic(Map bundle, String key) { - String result = ''; - final Map attributesMap = bundle['@$key'] as Map; +String generateDateFormattingLogic(Map arbBundle, String resourceId) { + final StringBuffer result = StringBuffer(); + final Map attributesMap = arbBundle['@$resourceId'] as Map; if (attributesMap != null && attributesMap.containsKey('placeholders')) { final Map placeholders = attributesMap['placeholders'] as Map; for (final String placeholder in placeholders.keys) { final dynamic value = placeholders[placeholder]; - if ( - value is Map && - _isDateParameter(value) && - _containsFormatKey(value, placeholder) && - _dateParameterIsValid(value, placeholder) - ) { - result += ''' + if (value is Map && _isValidDateFormat(value, placeholder)) { + result.write(''' final DateFormat ${placeholder}DateFormat = DateFormat.${value['format']}(_localeName); final String ${placeholder}String = ${placeholder}DateFormat.format($placeholder); -'''; +'''); } } } - return result; + return result.toString(); } -List genIntlMethodArgs(Map bundle, String key) { - final List attributes = ['name: \'$key\'']; - final Map attributesMap = bundle['@$key'] as Map; +String generateNumberFormattingLogic(Map arbBundle, String resourceId) { + final Map attributesMap = arbBundle['@$resourceId'] as Map; + if (attributesMap != null && attributesMap.containsKey('placeholders')) { + final StringBuffer result = StringBuffer(); + final Map placeholders = attributesMap['placeholders'] as Map; + final StringBuffer optionalParametersString = StringBuffer(); + for (final String placeholder in placeholders.keys) { + final dynamic value = placeholders[placeholder]; + if (value is Map && _isValidNumberFormat(value, placeholder)) { + if (value.containsKey('optionalParameters')) { + final Map optionalParameters = value['optionalParameters'] as Map; + for (final String parameter in optionalParameters.keys) + optionalParametersString.write('\n $parameter: ${optionalParameters[parameter]},'); + } + + result.write(''' + + final NumberFormat ${placeholder}NumberFormat = NumberFormat.${value['format']}( + locale: _localeName,@optionalParameters + ); + final String ${placeholder}String = ${placeholder}NumberFormat.format($placeholder); +'''); + } + } + + return result + .toString() + .replaceAll('@optionalParameters', optionalParametersString.toString()); + } + + return ''; +} + +bool _isValidDateFormat(Map value, String placeholder) { + return _isDateParameter(value) + && _containsFormatKey(value, placeholder) + && _isValidDateParameter(value, placeholder); +} + +bool _isValidNumberFormat(Map value, String placeholder) { + return _isNumberParameter(value) + && _containsFormatKey(value, placeholder) + && _isValidNumberParameter(value, placeholder); +} + +bool _isValidPlaceholder(Map value, String placeholder) { + return _isValidDateFormat(value, placeholder) || _isValidNumberFormat(value, placeholder); +} + +List genIntlMethodArgs(Map arbBundle, String resourceId) { + final List attributes = ['name: \'$resourceId\'']; + final Map attributesMap = arbBundle['@$resourceId'] as Map; if (attributesMap != null) { if (attributesMap.containsKey('description')) { final String description = attributesMap['description'] as String; @@ -304,12 +384,7 @@ List genIntlMethodArgs(Map bundle, String key) { final List argumentList = []; for (final String placeholder in placeholders.keys) { final dynamic value = placeholders[placeholder]; - if ( - value is Map && - _isDateParameter(value) && - _containsFormatKey(value, placeholder) && - _dateParameterIsValid(value, placeholder) - ) { + if (value is Map && _isValidPlaceholder(value, placeholder)) { argumentList.add('${placeholder}String'); } else { argumentList.add(placeholder); @@ -323,14 +398,14 @@ List genIntlMethodArgs(Map bundle, String key) { return attributes; } -String genSimpleMethod(Map bundle, String key) { - String genSimpleMethodMessage(Map bundle, String key) { - String message = bundle[key] as String; - final Map attributesMap = bundle['@$key'] as Map; +String genSimpleMethod(Map arbBundle, String resourceId) { + String genSimpleMethodMessage(Map arbBundle, String resourceId) { + String message = arbBundle[resourceId] as String; + final Map attributesMap = arbBundle['@$resourceId'] as Map; final Map placeholders = attributesMap['placeholders'] as Map; for (final String placeholder in placeholders.keys) { final dynamic value = placeholders[placeholder]; - if (value is Map && _isDateParameter(value)) { + if (value is Map && (_isDateParameter(value) || _isNumberParameter(value))) { message = message.replaceAll('{$placeholder}', '\$${placeholder}String'); } else { message = message.replaceAll('{$placeholder}', '\$$placeholder'); @@ -339,26 +414,27 @@ String genSimpleMethod(Map bundle, String key) { return generateString(message); } - final Map attributesMap = bundle['@$key'] as Map; + final Map attributesMap = arbBundle['@$resourceId'] as Map; if (attributesMap == null) throw L10nException( - 'Resource attribute "@$key" was not found. Please ensure that each ' + 'Resource attribute "@$resourceId" was not found. Please ensure that each ' 'resource id has a corresponding resource attribute.' ); if (attributesMap.containsKey('placeholders')) { return simpleMethodTemplate - .replaceAll('@methodName', key) - .replaceAll('@methodParameters', genMethodParameters(bundle, key, 'Object').join(', ')) - .replaceAll('@dateFormatting', generateDateFormattingLogic(bundle, key)) - .replaceAll('@message', '${genSimpleMethodMessage(bundle, key)}') - .replaceAll('@intlMethodArgs', genIntlMethodArgs(bundle, key).join(',\n ')); + .replaceAll('@methodName', resourceId) + .replaceAll('@methodParameters', genMethodParameters(arbBundle, resourceId, 'Object').join(', ')) + .replaceAll('@dateFormatting', generateDateFormattingLogic(arbBundle, resourceId)) + .replaceAll('@numberFormatting', generateNumberFormattingLogic(arbBundle, resourceId)) + .replaceAll('@message', '${genSimpleMethodMessage(arbBundle, resourceId)}') + .replaceAll('@intlMethodArgs', genIntlMethodArgs(arbBundle, resourceId).join(',\n ')); } return getterMethodTemplate - .replaceAll('@methodName', key) - .replaceAll('@message', '${generateString(bundle[key] as String)}') - .replaceAll('@intlMethodArgs', genIntlMethodArgs(bundle, key).join(',\n ')); + .replaceAll('@methodName', resourceId) + .replaceAll('@message', '${generateString(arbBundle[resourceId] as String)}') + .replaceAll('@intlMethodArgs', genIntlMethodArgs(arbBundle, resourceId).join(',\n ')); } String genPluralMethod(Map arbBundle, String resourceId) { @@ -413,7 +489,7 @@ String genPluralMethod(Map arbBundle, String resourceId) { String argValue = match.group(2); for (final String placeholder in placeholders) { final dynamic value = placeholdersMap[placeholder]; - if (value is Map && _isDateParameter(value)) { + if (value is Map && (_isDateParameter(value) || _isNumberParameter(value))) { argValue = argValue.replaceAll('#$placeholder#', '\$${placeholder}String'); } else { argValue = argValue.replaceAll('#$placeholder#', '\$$placeholder'); @@ -427,6 +503,7 @@ String genPluralMethod(Map arbBundle, String resourceId) { .replaceAll('@methodName', resourceId) .replaceAll('@methodParameters', genPluralMethodParameters(placeholders, countPlaceholder, resourceId).join(', ')) .replaceAll('@dateFormatting', generateDateFormattingLogic(arbBundle, resourceId)) + .replaceAll('@numberFormatting', generateNumberFormattingLogic(arbBundle, resourceId)) .replaceAll('@intlMethodArgs', methodArgs.join(',\n ')); } diff --git a/dev/tools/test/localization/gen_l10n_test.dart b/dev/tools/test/localization/gen_l10n_test.dart index 5ff906505b..4cddf72543 100644 --- a/dev/tools/test/localization/gen_l10n_test.dart +++ b/dev/tools/test/localization/gen_l10n_test.dart @@ -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: [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: [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: [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}}",