mirror of
https://github.com/flutter/flutter
synced 2024-10-13 03:32:55 +00:00
Add Message.isPlural, Message.getCountPlaceholder() to gen_l10n_types (#50132)
This commit is contained in:
parent
94c7e823be
commit
c3e0f2d462
|
@ -286,9 +286,17 @@ String generatePluralMethod(Message message) {
|
|||
'other': 'other'
|
||||
};
|
||||
|
||||
final String countPlaceholder = message.value.split(',')[0].substring(1);
|
||||
final Placeholder countPlaceholder = message.getCountPlaceholder();
|
||||
if (countPlaceholder == null) {
|
||||
throw L10nException(
|
||||
'Unable to find the count placeholder for the plural message: ${message.resourceId}.\n'
|
||||
'Check to see if the plural message is in the proper ICU syntax format '
|
||||
'and ensure that placeholders are properly specified.'
|
||||
);
|
||||
}
|
||||
|
||||
final List<String> intlMethodArgs = <String>[
|
||||
countPlaceholder,
|
||||
countPlaceholder.name,
|
||||
'locale: _localeName',
|
||||
...generateIntlMethodArgs(message),
|
||||
];
|
||||
|
@ -311,7 +319,7 @@ String generatePluralMethod(Message message) {
|
|||
|
||||
List<String> generatePluralMethodParameters([String type]) {
|
||||
return message.placeholders.map((Placeholder placeholder) {
|
||||
final String placeholderType = placeholder.name == countPlaceholder ? 'int' : (type ?? placeholder.type);
|
||||
final String placeholderType = placeholder == countPlaceholder ? 'int' : (type ?? placeholder.type);
|
||||
return '$placeholderType ${placeholder.name}';
|
||||
}).toList();
|
||||
}
|
||||
|
@ -346,7 +354,6 @@ class LocalizationsGenerator {
|
|||
|
||||
static RegExp arbFilenameLocaleRE = RegExp(r'^[^_]*_(\w+)\.arb$');
|
||||
static RegExp arbFilenameRE = RegExp(r'(\w+)\.arb$');
|
||||
static RegExp pluralValueRE = RegExp(r'^\s*\{[\w\s,]*,\s*plural\s*,');
|
||||
|
||||
final file.FileSystem _fs;
|
||||
|
||||
|
@ -673,7 +680,7 @@ class LocalizationsGenerator {
|
|||
}
|
||||
|
||||
final Message message = Message(bundle, key);
|
||||
if (pluralValueRE.hasMatch(message.value))
|
||||
if (message.isPlural)
|
||||
classMethods.add(generatePluralMethod(message));
|
||||
else
|
||||
classMethods.add(genSimpleMethod(message));
|
||||
|
|
|
@ -81,15 +81,32 @@ class Message {
|
|||
assert(resourceId != null && resourceId.isNotEmpty),
|
||||
value = _value(bundle, resourceId),
|
||||
description = _description(bundle, resourceId),
|
||||
placeholders = _placeholders(bundle, resourceId);
|
||||
placeholders = _placeholders(bundle, resourceId),
|
||||
_pluralMatch = _pluralRE.firstMatch(_value(bundle, resourceId));
|
||||
|
||||
static final RegExp _pluralRE = RegExp(r'\s*\{([\w\s,]*),\s*plural\s*,');
|
||||
|
||||
final String resourceId;
|
||||
final String value;
|
||||
final String description;
|
||||
final List<Placeholder> placeholders;
|
||||
final RegExpMatch _pluralMatch;
|
||||
|
||||
bool get isPlural => _pluralMatch != null && _pluralMatch.groupCount == 1;
|
||||
|
||||
bool get placeholdersRequireFormatting => placeholders.any((Placeholder p) => p.requiresFormatting);
|
||||
|
||||
Placeholder getCountPlaceholder() {
|
||||
assert(isPlural);
|
||||
final String countPlaceholderName = _pluralMatch[1];
|
||||
return placeholders.firstWhere(
|
||||
(Placeholder p) => p.name == countPlaceholderName,
|
||||
orElse: () {
|
||||
throw L10nException('Cannot find the $countPlaceholderName placeholder in plural message "$resourceId".');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
static String _value(Map<String, dynamic> bundle, String resourceId) {
|
||||
final dynamic value = bundle[resourceId];
|
||||
if (value == null)
|
||||
|
|
Loading…
Reference in a new issue