gen_l10n Number Format Handling (#47706)

This commit is contained in:
Shi-Hao Hong
2020-01-07 22:38:02 -08:00
committed by Flutter GitHub Bot
parent 846dce92ff
commit b038b40941
2 changed files with 332 additions and 52 deletions

View File

@@ -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<String> allowableDateFormats = <String>{
's',
};
bool _isDateParameter(Map<String, dynamic> 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:
//
// * <https://pub.dev/packages/intl>
// * <https://pub.dev/documentation/intl/latest/intl/NumberFormat-class.html>
const Set<String> allowableNumberFormats = <String>{
'compact',
'compactLong',
'decimalPattern',
'decimalPercentPattern',
'percentPattern',
'scientificPattern',
};
bool _dateParameterIsValid(Map<String, dynamic> placeholderValue, String placeholder) {
bool _isDateParameter(Map<String, dynamic> placeholderValue) => placeholderValue['type'] == 'DateTime';
bool _isNumberParameter(Map<String, dynamic> placeholderValue) => placeholderValue['type'] == 'Number';
bool _containsFormatKey(Map<String, dynamic> 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<String, dynamic> 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<String, dynamic> placeholderValue, String placeholder) {
if (placeholderValue.containsKey('format'))
bool _isValidNumberParameter(Map<String, dynamic> 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<String> genMethodParameters(Map<String, dynamic> bundle, String key, String type) {
final Map<String, dynamic> attributesMap = bundle['@$key'] as Map<String, dynamic>;
List<String> genMethodParameters(Map<String, dynamic> bundle, String resourceId, String type) {
final Map<String, dynamic> attributesMap = bundle['@$resourceId'] as Map<String, dynamic>;
if (attributesMap != null && attributesMap.containsKey('placeholders')) {
final Map<String, dynamic> placeholders = attributesMap['placeholders'] as Map<String, dynamic>;
return placeholders.keys.map((String parameter) => '$type $parameter').toList();
@@ -265,34 +301,78 @@ List<String> genPluralMethodParameters(Iterable<String> placeholderKeys, String
}).toList();
}
String generateDateFormattingLogic(Map<String, dynamic> bundle, String key) {
String result = '';
final Map<String, dynamic> attributesMap = bundle['@$key'] as Map<String, dynamic>;
String generateDateFormattingLogic(Map<String, dynamic> arbBundle, String resourceId) {
final StringBuffer result = StringBuffer();
final Map<String, dynamic> attributesMap = arbBundle['@$resourceId'] as Map<String, dynamic>;
if (attributesMap != null && attributesMap.containsKey('placeholders')) {
final Map<String, dynamic> placeholders = attributesMap['placeholders'] as Map<String, dynamic>;
for (final String placeholder in placeholders.keys) {
final dynamic value = placeholders[placeholder];
if (
value is Map<String, dynamic> &&
_isDateParameter(value) &&
_containsFormatKey(value, placeholder) &&
_dateParameterIsValid(value, placeholder)
) {
result += '''
if (value is Map<String, dynamic> && _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<String> genIntlMethodArgs(Map<String, dynamic> bundle, String key) {
final List<String> attributes = <String>['name: \'$key\''];
final Map<String, dynamic> attributesMap = bundle['@$key'] as Map<String, dynamic>;
String generateNumberFormattingLogic(Map<String, dynamic> arbBundle, String resourceId) {
final Map<String, dynamic> attributesMap = arbBundle['@$resourceId'] as Map<String, dynamic>;
if (attributesMap != null && attributesMap.containsKey('placeholders')) {
final StringBuffer result = StringBuffer();
final Map<String, dynamic> placeholders = attributesMap['placeholders'] as Map<String, dynamic>;
final StringBuffer optionalParametersString = StringBuffer();
for (final String placeholder in placeholders.keys) {
final dynamic value = placeholders[placeholder];
if (value is Map<String, dynamic> && _isValidNumberFormat(value, placeholder)) {
if (value.containsKey('optionalParameters')) {
final Map<String, dynamic> optionalParameters = value['optionalParameters'] as Map<String, dynamic>;
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<String, dynamic> value, String placeholder) {
return _isDateParameter(value)
&& _containsFormatKey(value, placeholder)
&& _isValidDateParameter(value, placeholder);
}
bool _isValidNumberFormat(Map<String, dynamic> value, String placeholder) {
return _isNumberParameter(value)
&& _containsFormatKey(value, placeholder)
&& _isValidNumberParameter(value, placeholder);
}
bool _isValidPlaceholder(Map<String, dynamic> value, String placeholder) {
return _isValidDateFormat(value, placeholder) || _isValidNumberFormat(value, placeholder);
}
List<String> genIntlMethodArgs(Map<String, dynamic> arbBundle, String resourceId) {
final List<String> attributes = <String>['name: \'$resourceId\''];
final Map<String, dynamic> attributesMap = arbBundle['@$resourceId'] as Map<String, dynamic>;
if (attributesMap != null) {
if (attributesMap.containsKey('description')) {
final String description = attributesMap['description'] as String;
@@ -304,12 +384,7 @@ List<String> genIntlMethodArgs(Map<String, dynamic> bundle, String key) {
final List<String> argumentList = <String>[];
for (final String placeholder in placeholders.keys) {
final dynamic value = placeholders[placeholder];
if (
value is Map<String, dynamic> &&
_isDateParameter(value) &&
_containsFormatKey(value, placeholder) &&
_dateParameterIsValid(value, placeholder)
) {
if (value is Map<String, dynamic> && _isValidPlaceholder(value, placeholder)) {
argumentList.add('${placeholder}String');
} else {
argumentList.add(placeholder);
@@ -323,14 +398,14 @@ List<String> genIntlMethodArgs(Map<String, dynamic> bundle, String key) {
return attributes;
}
String genSimpleMethod(Map<String, dynamic> bundle, String key) {
String genSimpleMethodMessage(Map<String, dynamic> bundle, String key) {
String message = bundle[key] as String;
final Map<String, dynamic> attributesMap = bundle['@$key'] as Map<String, dynamic>;
String genSimpleMethod(Map<String, dynamic> arbBundle, String resourceId) {
String genSimpleMethodMessage(Map<String, dynamic> arbBundle, String resourceId) {
String message = arbBundle[resourceId] as String;
final Map<String, dynamic> attributesMap = arbBundle['@$resourceId'] as Map<String, dynamic>;
final Map<String, dynamic> placeholders = attributesMap['placeholders'] as Map<String, dynamic>;
for (final String placeholder in placeholders.keys) {
final dynamic value = placeholders[placeholder];
if (value is Map<String, dynamic> && _isDateParameter(value)) {
if (value is Map<String, dynamic> && (_isDateParameter(value) || _isNumberParameter(value))) {
message = message.replaceAll('{$placeholder}', '\$${placeholder}String');
} else {
message = message.replaceAll('{$placeholder}', '\$$placeholder');
@@ -339,26 +414,27 @@ String genSimpleMethod(Map<String, dynamic> bundle, String key) {
return generateString(message);
}
final Map<String, dynamic> attributesMap = bundle['@$key'] as Map<String, dynamic>;
final Map<String, dynamic> attributesMap = arbBundle['@$resourceId'] as Map<String, dynamic>;
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<String, dynamic> arbBundle, String resourceId) {
@@ -413,7 +489,7 @@ String genPluralMethod(Map<String, dynamic> arbBundle, String resourceId) {
String argValue = match.group(2);
for (final String placeholder in placeholders) {
final dynamic value = placeholdersMap[placeholder];
if (value is Map<String, dynamic> && _isDateParameter(value)) {
if (value is Map<String, dynamic> && (_isDateParameter(value) || _isNumberParameter(value))) {
argValue = argValue.replaceAll('#$placeholder#', '\$${placeholder}String');
} else {
argValue = argValue.replaceAll('#$placeholder#', '\$$placeholder');
@@ -427,6 +503,7 @@ String genPluralMethod(Map<String, dynamic> 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 '));
}