[gen_l10n] Support plurals and selects inside string (#86167)
This commit is contained in:
parent
b6419e8e89
commit
f568d929db
@ -229,15 +229,102 @@ String _generatePluralMethod(Message message, String translationForMessage) {
|
||||
|
||||
final String comment = message.description ?? 'No description provided in @${message.resourceId}';
|
||||
|
||||
return pluralMethodTemplate
|
||||
if (translationForMessage.startsWith('{') && translationForMessage.endsWith('}')) {
|
||||
return pluralMethodTemplate
|
||||
.replaceAll('@(comment)', comment)
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(count)', countPlaceholder.name)
|
||||
.replaceAll('@(pluralLogicArgs)', pluralLogicArgs.join(',\n'))
|
||||
.replaceAll('@(none)\n', '');
|
||||
}
|
||||
|
||||
const String variable = 'pluralString';
|
||||
final String string = _replaceWithVariable(translationForMessage, variable);
|
||||
return pluralMethodTemplateInString
|
||||
.replaceAll('@(comment)', comment)
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(dateFormatting)', generateDateFormattingLogic(message))
|
||||
.replaceAll('@(numberFormatting)', generateNumberFormattingLogic(message))
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(variable)', variable)
|
||||
.replaceAll('@(count)', countPlaceholder.name)
|
||||
.replaceAll('@(pluralLogicArgs)', pluralLogicArgs.join(',\n'))
|
||||
.replaceAll('@(none)\n', '');
|
||||
.replaceAll('@(none)\n', '')
|
||||
.replaceAll('@(string)', string);
|
||||
}
|
||||
|
||||
String _replaceWithVariable(String translation, String variable) {
|
||||
String prefix = generateString(translation.substring(0, translation.indexOf('{')));
|
||||
prefix = prefix.substring(0, prefix.length - 1);
|
||||
String suffix = generateString(translation.substring(translation.lastIndexOf('}') + 1));
|
||||
suffix = suffix.substring(1);
|
||||
|
||||
// escape variable when the suffix can be combined with the variable
|
||||
if (suffix.isNotEmpty && !suffix.startsWith(' ')) {
|
||||
variable = '{$variable}';
|
||||
}
|
||||
return prefix + r'$' + variable + suffix;
|
||||
}
|
||||
|
||||
String _generateSelectMethod(Message message, String translationForMessage) {
|
||||
if (message.placeholders.isEmpty) {
|
||||
throw L10nException(
|
||||
'Unable to find placeholders for the select message: ${message.resourceId}.\n'
|
||||
'Check to see if the select message is in the proper ICU syntax format '
|
||||
'and ensure that placeholders are properly specified.'
|
||||
);
|
||||
}
|
||||
|
||||
final List<String> cases = <String>[];
|
||||
|
||||
final RegExpMatch? selectMatch =
|
||||
LocalizationsGenerator._selectRE.firstMatch(translationForMessage);
|
||||
String? choice;
|
||||
if (selectMatch != null && selectMatch.groupCount == 2) {
|
||||
choice = selectMatch.group(1);
|
||||
final String pattern = selectMatch.group(2)!;
|
||||
final RegExp patternRE = RegExp(r'\s*([\w\d]+)\s*\{(.*?)\}');
|
||||
for (final RegExpMatch patternMatch in patternRE.allMatches(pattern)) {
|
||||
if (patternMatch.groupCount == 2) {
|
||||
final String value = patternMatch.group(2)!
|
||||
.replaceAll("'", r"\'")
|
||||
.replaceAll('"', r'\"');
|
||||
cases.add(
|
||||
" '${patternMatch.group(1)}': '$value'",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final List<String> parameters = message.placeholders.map((Placeholder placeholder) {
|
||||
final String placeholderType = placeholder.type ?? 'object';
|
||||
return '$placeholderType ${placeholder.name}';
|
||||
}).toList();
|
||||
|
||||
final String description = message.description ?? 'No description provided in @${message.resourceId}';
|
||||
|
||||
if (translationForMessage.startsWith('{') && translationForMessage.endsWith('}')) {
|
||||
return selectMethodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(choice)', choice!)
|
||||
.replaceAll('@(cases)', cases.join(',\n').trim())
|
||||
.replaceAll('@(description)', description);
|
||||
}
|
||||
|
||||
const String variable = 'selectString';
|
||||
final String string = _replaceWithVariable(translationForMessage, variable);
|
||||
return selectMethodTemplateInString
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
.replaceAll('@(parameters)', parameters.join(', '))
|
||||
.replaceAll('@(variable)', variable)
|
||||
.replaceAll('@(choice)', choice!)
|
||||
.replaceAll('@(cases)', cases.join(',\n').trim())
|
||||
.replaceAll('@(description)', description)
|
||||
.replaceAll('@(string)', string);
|
||||
}
|
||||
|
||||
bool _needsCurlyBracketStringInterpolation(String messageString, String placeholder) {
|
||||
@ -305,6 +392,10 @@ String _generateMethod(Message message, String translationForMessage) {
|
||||
return _generatePluralMethod(message, translationForMessage);
|
||||
}
|
||||
|
||||
if (message.isSelect) {
|
||||
return _generateSelectMethod(message, translationForMessage);
|
||||
}
|
||||
|
||||
if (message.placeholdersRequireFormatting) {
|
||||
return formatMethodTemplate
|
||||
.replaceAll('@(name)', message.resourceId)
|
||||
@ -741,6 +832,8 @@ class LocalizationsGenerator {
|
||||
@visibleForTesting
|
||||
final bool areResourceAttributesRequired;
|
||||
|
||||
static final RegExp _selectRE = RegExp(r'\{([\w\s,]*),\s*select\s*,\s*([\w\d]+\s*\{.*\})+\s*\}');
|
||||
|
||||
static bool _isNotReadable(FileStat fileStat) {
|
||||
final String rawStatString = fileStat.modeString();
|
||||
// Removes potential prepended permission bits, such as '(suid)' and '(guid)'.
|
||||
@ -1168,7 +1261,11 @@ class LocalizationsGenerator {
|
||||
.replaceAll('\n\n\n', '\n\n');
|
||||
}
|
||||
|
||||
bool _requiresIntlImport() => _allMessages.any((Message message) => message.isPlural || message.placeholdersRequireFormatting);
|
||||
bool _requiresIntlImport() => _allMessages.any((Message message) {
|
||||
return message.isPlural
|
||||
|| message.isSelect
|
||||
|| message.placeholdersRequireFormatting;
|
||||
});
|
||||
|
||||
void writeOutputFiles(Logger logger, { bool isFromYaml = false }) {
|
||||
// First, generate the string contents of all necessary files.
|
||||
|
@ -156,6 +156,46 @@ const String pluralMethodTemplate = '''
|
||||
);
|
||||
}''';
|
||||
|
||||
const String pluralMethodTemplateInString = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
@(dateFormatting)
|
||||
@(numberFormatting)
|
||||
final String @(variable) = intl.Intl.pluralLogic(
|
||||
@(count),
|
||||
locale: localeName,
|
||||
@(pluralLogicArgs),
|
||||
);
|
||||
|
||||
return @(string);
|
||||
}''';
|
||||
|
||||
const String selectMethodTemplate = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
return intl.Intl.select(
|
||||
@(choice),
|
||||
{
|
||||
@(cases)
|
||||
},
|
||||
desc: '@(description)'
|
||||
);
|
||||
}''';
|
||||
|
||||
const String selectMethodTemplateInString = '''
|
||||
@override
|
||||
String @(name)(@(parameters)) {
|
||||
final String @(variable) = intl.Intl.select(
|
||||
@(choice),
|
||||
{
|
||||
@(cases)
|
||||
},
|
||||
desc: '@(description)'
|
||||
);
|
||||
|
||||
return @(string);
|
||||
}''';
|
||||
|
||||
const String classFileTemplate = '''
|
||||
@(header)
|
||||
|
||||
|
@ -273,17 +273,21 @@ class Message {
|
||||
value = _value(bundle, resourceId),
|
||||
description = _description(bundle, resourceId, isResourceAttributeRequired),
|
||||
placeholders = _placeholders(bundle, resourceId, isResourceAttributeRequired),
|
||||
_pluralMatch = _pluralRE.firstMatch(_value(bundle, resourceId));
|
||||
_pluralMatch = _pluralRE.firstMatch(_value(bundle, resourceId)),
|
||||
_selectMatch = _selectRE.firstMatch(_value(bundle, resourceId));
|
||||
|
||||
static final RegExp _pluralRE = RegExp(r'\s*\{([\w\s,]*),\s*plural\s*,');
|
||||
static final RegExp _selectRE = RegExp(r'\s*\{([\w\s,]*),\s*select\s*,');
|
||||
|
||||
final String resourceId;
|
||||
final String value;
|
||||
final String? description;
|
||||
final List<Placeholder> placeholders;
|
||||
final RegExpMatch? _pluralMatch;
|
||||
final RegExpMatch? _selectMatch;
|
||||
|
||||
bool get isPlural => _pluralMatch != null && _pluralMatch!.groupCount == 1;
|
||||
bool get isSelect => _selectMatch != null && _selectMatch!.groupCount == 1;
|
||||
|
||||
bool get placeholdersRequireFormatting => placeholders.any((Placeholder p) => p.requiresFormatting);
|
||||
|
||||
@ -331,13 +335,21 @@ class Message {
|
||||
);
|
||||
}
|
||||
|
||||
final RegExpMatch? pluralRegExp = _pluralRE.firstMatch(_value(bundle, resourceId));
|
||||
final bool isPlural = pluralRegExp != null && pluralRegExp.groupCount == 1;
|
||||
if (attributes == null && isPlural) {
|
||||
throw L10nException(
|
||||
'Resource attribute "@$resourceId" was not found. Please '
|
||||
'ensure that plural resources have a corresponding @resource.'
|
||||
);
|
||||
if (attributes == null) {
|
||||
|
||||
void _throwEmptyAttributes(final RegExp regExp, final String type) {
|
||||
final RegExpMatch? match = regExp.firstMatch(_value(bundle, resourceId));
|
||||
final bool isMatch = match != null && match.groupCount == 1;
|
||||
if (isMatch) {
|
||||
throw L10nException(
|
||||
'Resource attribute "@$resourceId" was not found. Please '
|
||||
'ensure that $type resources have a corresponding @resource.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
_throwEmptyAttributes(_pluralRE, 'plural');
|
||||
_throwEmptyAttributes(_selectRE, 'select');
|
||||
}
|
||||
|
||||
return attributes as Map<String, Object?>?;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -118,40 +118,50 @@ void main() {
|
||||
"#l10n 63 (Flutter's amazing, times 2!)\n"
|
||||
'#l10n 64 (Flutter is "amazing"!)\n'
|
||||
'#l10n 65 (Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 66 (--- es ---)\n'
|
||||
'#l10n 67 (ES - Hello world)\n'
|
||||
'#l10n 68 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 69 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 70 (ES - Hello Mundo)\n'
|
||||
'#l10n 71 (ES - Hola Mundo)\n'
|
||||
'#l10n 72 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 73 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 74 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 75 (ES - Hello for 123)\n'
|
||||
'#l10n 76 (ES - Hello)\n'
|
||||
'#l10n 77 (ES - Hello World)\n'
|
||||
'#l10n 78 (ES - Hello two worlds)\n'
|
||||
'#l10n 79 (ES - Hello)\n'
|
||||
'#l10n 80 (ES - Hello nuevo World)\n'
|
||||
'#l10n 81 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 82 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 83 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 84 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 85 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 86 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 87 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 88 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 89 (ES - \$!)\n'
|
||||
'#l10n 90 (ES - One \$)\n'
|
||||
"#l10n 91 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 92 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 93 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 94 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 95 (--- es_419 ---)\n'
|
||||
'#l10n 96 (ES 419 - Hello World)\n'
|
||||
'#l10n 97 (ES 419 - Hello)\n'
|
||||
'#l10n 98 (ES 419 - Hello World)\n'
|
||||
'#l10n 99 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n 66 (16 wheel truck)\n'
|
||||
"#l10n 67 (Sedan's elegance)\n"
|
||||
'#l10n 68 (Cabriolet has "acceleration")\n'
|
||||
'#l10n 69 (Oh, she found 1 item!)\n'
|
||||
'#l10n 70 (Indeed, they like Flutter!)\n'
|
||||
'#l10n 71 (--- es ---)\n'
|
||||
'#l10n 72 (ES - Hello world)\n'
|
||||
'#l10n 73 (ES - Hello _NEWLINE_ World)\n'
|
||||
'#l10n 74 (ES - Hola \$ Mundo)\n'
|
||||
'#l10n 75 (ES - Hello Mundo)\n'
|
||||
'#l10n 76 (ES - Hola Mundo)\n'
|
||||
'#l10n 77 (ES - Hello World on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 78 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
|
||||
'#l10n 79 (ES - Hello World from 1960 to 2020)\n'
|
||||
'#l10n 80 (ES - Hello for 123)\n'
|
||||
'#l10n 81 (ES - Hello)\n'
|
||||
'#l10n 82 (ES - Hello World)\n'
|
||||
'#l10n 83 (ES - Hello two worlds)\n'
|
||||
'#l10n 84 (ES - Hello)\n'
|
||||
'#l10n 85 (ES - Hello nuevo World)\n'
|
||||
'#l10n 86 (ES - Hello two nuevo worlds)\n'
|
||||
'#l10n 87 (ES - Hello on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 88 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 89 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
|
||||
'#l10n 90 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
|
||||
'#l10n 91 (ES - Hello World of 101 citizens)\n'
|
||||
'#l10n 92 (ES - Hello two worlds with 102 total citizens)\n'
|
||||
'#l10n 93 (ES - [Hola] -Mundo- #123#)\n'
|
||||
'#l10n 94 (ES - \$!)\n'
|
||||
'#l10n 95 (ES - One \$)\n'
|
||||
"#l10n 96 (ES - Flutter's amazing!)\n"
|
||||
"#l10n 97 (ES - Flutter's amazing, times 2!)\n"
|
||||
'#l10n 98 (ES - Flutter is "amazing"!)\n'
|
||||
'#l10n 99 (ES - Flutter is "amazing", times 2!)\n'
|
||||
'#l10n 100 (ES - 16 wheel truck)\n'
|
||||
"#l10n 101 (ES - Sedan's elegance)\n"
|
||||
'#l10n 102 (ES - Cabriolet has "acceleration")\n'
|
||||
'#l10n 103 (ES - Oh, she found ES - 1 itemES - !)\n'
|
||||
'#l10n 104 (ES - Indeed, ES - they like ES - Flutter!)\n'
|
||||
'#l10n 105 (--- es_419 ---)\n'
|
||||
'#l10n 106 (ES 419 - Hello World)\n'
|
||||
'#l10n 107 (ES 419 - Hello)\n'
|
||||
'#l10n 108 (ES 419 - Hello World)\n'
|
||||
'#l10n 109 (ES 419 - Hello two worlds)\n'
|
||||
'#l10n END\n'
|
||||
);
|
||||
}
|
||||
|
@ -225,6 +225,11 @@ class Home extends StatelessWidget {
|
||||
'${localizations.singleQuotePlural(2)}',
|
||||
'${localizations.doubleQuote}',
|
||||
'${localizations.doubleQuotePlural(2)}',
|
||||
"${localizations.vehicleSelect('truck')}",
|
||||
"${localizations.singleQuoteSelect('sedan')}",
|
||||
"${localizations.doubleQuoteSelect('cabriolet')}",
|
||||
"${localizations.pluralInString(1)}",
|
||||
"${localizations.selectInString('he')}",
|
||||
]);
|
||||
},
|
||||
),
|
||||
@ -266,6 +271,11 @@ class Home extends StatelessWidget {
|
||||
'${localizations.singleQuotePlural(2)}',
|
||||
'${localizations.doubleQuote}',
|
||||
'${localizations.doubleQuotePlural(2)}',
|
||||
"${localizations.vehicleSelect('truck')}",
|
||||
"${localizations.singleQuoteSelect('sedan')}",
|
||||
"${localizations.doubleQuoteSelect('cabriolet')}",
|
||||
"${localizations.pluralInString(1)}",
|
||||
"${localizations.selectInString('he')}",
|
||||
]);
|
||||
},
|
||||
),
|
||||
@ -608,6 +618,46 @@ void main() {
|
||||
"placeholders": {
|
||||
"count": {}
|
||||
}
|
||||
},
|
||||
|
||||
"vehicleSelect": "{vehicleType, select, sedan{Sedan} cabriolet{Solid roof cabriolet} truck{16 wheel truck} other{Other}}",
|
||||
"@vehicleSelect": {
|
||||
"description": "A select message.",
|
||||
"placeholders": {
|
||||
"vehicleType": {}
|
||||
}
|
||||
},
|
||||
|
||||
"singleQuoteSelect": "{vehicleType, select, sedan{Sedan's elegance} cabriolet{Cabriolet' acceleration} truck{truck's heavy duty} other{Other's mirrors!}}",
|
||||
"@singleQuoteSelect": {
|
||||
"description": "A select message with a single quote.",
|
||||
"placeholders": {
|
||||
"vehicleType": {}
|
||||
}
|
||||
},
|
||||
|
||||
"doubleQuoteSelect": "{vehicleType, select, sedan{Sedan has \"elegance\"} cabriolet{Cabriolet has \"acceleration\"} truck{truck is \"heavy duty\"} other{Other have \"mirrors\"!}}",
|
||||
"@doubleQuoteSelect": {
|
||||
"description": "A select message with double quote.",
|
||||
"placeholders": {
|
||||
"vehicleType": {}
|
||||
}
|
||||
},
|
||||
|
||||
"pluralInString": "Oh, she found {count, plural, =1 {1 item} other {all {count} items} }!",
|
||||
"@pluralInString": {
|
||||
"description": "A plural message with prefix and suffix strings.",
|
||||
"placeholders": {
|
||||
"count": {}
|
||||
}
|
||||
},
|
||||
|
||||
"selectInString": "Indeed, {gender, select, male {he likes} female {she likes} other {they like} } Flutter!",
|
||||
"@selectInString": {
|
||||
"description": "A select message with prefix and suffix strings.",
|
||||
"placeholders": {
|
||||
"gender": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
''';
|
||||
@ -646,13 +696,18 @@ void main() {
|
||||
"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}}",
|
||||
"helloWorldInterpolation": "ES - [{hello}] #{world}#",
|
||||
"helloWorldsInterpolation": "ES - {count,plural, other {ES - [{hello}] -{world}- #{count}#}}",
|
||||
"helloWorldsInterpolation": "{count,plural, other {ES - [{hello}] -{world}- #{count}#}}",
|
||||
"dollarSign": "ES - $!",
|
||||
"dollarSignPlural": "{count,plural, =1{ES - One $} other{ES - Many $}}",
|
||||
"singleQuote": "ES - Flutter's amazing!",
|
||||
"singleQuotePlural": "{count,plural, =1{ES - Flutter's amazing, times 1!} other{ES - Flutter's amazing, times {count}!}}",
|
||||
"doubleQuote": "ES - Flutter is \"amazing\"!",
|
||||
"doubleQuotePlural": "{count,plural, =1{ES - Flutter is \"amazing\", times 1!} other{ES - Flutter is \"amazing\", times {count}!}}"
|
||||
"doubleQuotePlural": "{count,plural, =1{ES - Flutter is \"amazing\", times 1!} other{ES - Flutter is \"amazing\", times {count}!}}",
|
||||
"vehicleSelect": "{vehicleType, select, sedan{ES - Sedan} cabriolet{ES - Solid roof cabriolet} truck{ES - 16 wheel truck} other{ES - Other}}",
|
||||
"singleQuoteSelect": "{vehicleType, select, sedan{ES - Sedan's elegance} cabriolet{ES - Cabriolet' acceleration} truck{ES - truck's heavy duty} other{ES - Other's mirrors!}}",
|
||||
"doubleQuoteSelect": "{vehicleType, select, sedan{ES - Sedan has \"elegance\"} cabriolet{ES - Cabriolet has \"acceleration\"} truck{ES - truck is \"heavy duty\"} other{ES - Other have \"mirrors\"!}}",
|
||||
"pluralInString": "ES - Oh, she found {count, plural, =1 {ES - 1 item} other {ES - all {count} items} }ES - !",
|
||||
"selectInString": "ES - Indeed, {gender, select, male {ES - he likes} female {ES - she likes} other {ES - they like} } ES - Flutter!"
|
||||
}
|
||||
''';
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user