[gen_l10n] Support string list as preferred-supported-locales (#63649)

* [gen_l10n] Support string list as preferred-supported-locales, as documented.

* [gen_l10n] Convert preferredSupportedLocale to a list of strings. 

* [gen_l10n] Accept a multi option instead of a json string in the command line.
This commit is contained in:
Herbert Poul
2020-09-10 00:32:40 +01:00
committed by GitHub
parent 6d1c244b79
commit 8dfd42f6b7
5 changed files with 59 additions and 74 deletions

View File

@@ -56,7 +56,7 @@ void generateLocalizations({
templateArbFileName: templateArbFileName,
outputFileString: outputFileString,
classNameString: options.outputClass ?? 'AppLocalizations',
preferredSupportedLocaleString: options.preferredSupportedLocales,
preferredSupportedLocale: options.preferredSupportedLocales,
headerString: options.header,
headerFile: options?.headerFile?.toFilePath(),
useDeferredLoading: options.deferredLoading ?? false,
@@ -193,7 +193,7 @@ class LocalizationOptions {
final String outputClass;
/// The `--preferred-supported-locales` argument.
final String preferredSupportedLocales;
final List<String> preferredSupportedLocales;
/// The `--header-file` argument.
///
@@ -240,7 +240,7 @@ LocalizationOptions parseLocalizationsOptions({
untranslatedMessagesFile: _tryReadUri(yamlMap, 'untranslated-messages-file', logger),
header: _tryReadString(yamlMap, 'header', logger),
outputClass: _tryReadString(yamlMap, 'output-class', logger),
preferredSupportedLocales: _tryReadString(yamlMap, 'preferred-supported-locales', logger),
preferredSupportedLocales: _tryReadStringList(yamlMap, 'preferred-supported-locales', logger),
headerFile: _tryReadUri(yamlMap, 'header-file', logger),
deferredLoading: _tryReadBool(yamlMap, 'use-deferred-loading', logger),
useSyntheticPackage: _tryReadBool(yamlMap, 'synthetic-package', logger) ?? true,
@@ -273,6 +273,21 @@ String _tryReadString(YamlMap yamlMap, String key, Logger logger) {
return value as String;
}
List<String> _tryReadStringList(YamlMap yamlMap, String key, Logger logger) {
final Object value = yamlMap[key];
if (value == null) {
return null;
}
if (value is String) {
return <String>[value];
}
if (value is Iterable) {
return value.map((dynamic e) => e.toString()).toList();
}
logger.printError('"$value" must be String or List.');
throw Exception();
}
// Try to read a valid `Uri` or null from `yamlMap`, otherwise throw.
Uri _tryReadUri(YamlMap yamlMap, String key, Logger logger) {
final String value = _tryReadString(yamlMap, key, logger);

View File

@@ -65,14 +65,16 @@ class GenerateLocalizationsCommand extends FlutterCommand {
help: 'The Dart class name to use for the output localization and '
'localizations delegate classes.',
);
argParser.addOption(
argParser.addMultiOption(
'preferred-supported-locales',
valueHelp: 'locale',
help: 'The list of preferred supported locales for the application. '
'By default, the tool will generate the supported locales list in '
'alphabetical order. Use this flag if you would like to default to '
'a different locale. \n\n'
"For example, pass in ['en_US'] if you would like your app to "
'default to American English if a device supports it.',
'For example, pass in `en_US` if you would like your app to '
'default to American English if a device supports it.'
'(Pass this option multiple times for defining multiple items',
);
argParser.addOption(
'header',
@@ -169,7 +171,7 @@ class GenerateLocalizationsCommand extends FlutterCommand {
final String templateArbFileName = stringArg('template-arb-file');
final String untranslatedMessagesFile = stringArg('untranslated-messages-file');
final String classNameString = stringArg('output-class');
final String preferredSupportedLocaleString = stringArg('preferred-supported-locales');
final List<String> preferredSupportedLocale = stringsArg('preferred-supported-locales');
final String headerString = stringArg('header');
final String headerFile = stringArg('header-file');
final bool useDeferredLoading = boolArg('use-deferred-loading');
@@ -187,7 +189,7 @@ class GenerateLocalizationsCommand extends FlutterCommand {
templateArbFileName: templateArbFileName,
outputFileString: outputFileString,
classNameString: classNameString,
preferredSupportedLocaleString: preferredSupportedLocaleString,
preferredSupportedLocale: preferredSupportedLocale,
headerString: headerString,
headerFile: headerFile,
useDeferredLoading: useDeferredLoading,

View File

@@ -530,7 +530,7 @@ class LocalizationsGenerator {
String templateArbFileName,
String outputFileString,
String classNameString,
String preferredSupportedLocaleString,
List<String> preferredSupportedLocale,
String headerString,
String headerFile,
bool useDeferredLoading = false,
@@ -546,7 +546,7 @@ class LocalizationsGenerator {
);
setTemplateArbFile(templateArbFileName);
setBaseOutputFile(outputFileString);
setPreferredSupportedLocales(preferredSupportedLocaleString);
setPreferredSupportedLocales(preferredSupportedLocale);
_setHeader(headerString, headerFile);
_setUseDeferredLoading(useDeferredLoading);
className = classNameString;
@@ -707,18 +707,12 @@ class LocalizationsGenerator {
/// Sets [preferredSupportedLocales] so that this particular list of locales
/// will take priority over the other locales.
@visibleForTesting
void setPreferredSupportedLocales(String inputLocales) {
if (inputLocales == null || inputLocales.trim().isEmpty) {
void setPreferredSupportedLocales(List<String> inputLocales) {
if (inputLocales == null || inputLocales.isEmpty) {
_preferredSupportedLocales = const <LocaleInfo>[];
} else {
final List<dynamic> preferredLocalesStringList = json.decode(inputLocales) as List<dynamic>;
_preferredSupportedLocales = preferredLocalesStringList.map((dynamic localeString) {
if (localeString.runtimeType != String) {
throw L10nException('Incorrect runtime type for $localeString');
}
return LocaleInfo.fromString(
localeString.toString(),
);
_preferredSupportedLocales = inputLocales.map((String localeString) {
return LocaleInfo.fromString(localeString);
}).toList();
}
}