Cupertino localization step 8: create a gen_cupertino_localizations and generate one for cupertino english and french (#29824)

This commit is contained in:
xster
2019-04-24 12:40:17 -07:00
committed by GitHub
parent 96f15c74ad
commit c7a09a415d
10 changed files with 490 additions and 62 deletions

View File

@@ -0,0 +1,80 @@
import 'localizations_utils.dart';
HeaderGenerator generateCupertinoHeader = (String regenerateInstructions) {
return '''
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file has been automatically generated. Please do not edit it manually.
// To regenerate the file, use:
// $regenerateInstructions
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';
import 'package:intl/intl.dart' as intl;
import '../cupertino_localizations.dart';
// The classes defined here encode all of the translations found in the
// `flutter_localizations/lib/src/l10n/*.arb` files.
//
// These classes are constructed by the [getCupertinoTranslation] method at the
// bottom of this file, and used by the [_GlobalCupertinoLocalizationsDelegate.load]
// method defined in `flutter_localizations/lib/src/cupertino_localizations.dart`.''';
};
/// Returns the source of the constructor for a GlobalCupertinoLocalizations
/// subclass.
ConstructorGenerator generateCupertinoConstructor = (LocaleInfo locale) {
final String localeName = locale.originalString;
return '''
/// Create an instance of the translation bundle for ${describeLocale(localeName)}.
///
/// For details on the meaning of the arguments, see [GlobalCupertinoLocalizations].
const CupertinoLocalization${camelCase(locale)}({
String localeName = '$localeName',
@required intl.DateFormat fullYearFormat,
@required intl.DateFormat dayFormat,
@required intl.DateFormat mediumDateFormat,
@required intl.DateFormat singleDigitHourFormat,
@required intl.DateFormat singleDigitMinuteFormat,
@required intl.DateFormat doubleDigitMinuteFormat,
@required intl.DateFormat singleDigitSecondFormat,
@required intl.NumberFormat decimalFormat,
}) : super(
localeName: localeName,
fullYearFormat: fullYearFormat,
dayFormat: dayFormat,
mediumDateFormat: mediumDateFormat,
singleDigitHourFormat: singleDigitHourFormat,
singleDigitMinuteFormat: singleDigitMinuteFormat,
doubleDigitMinuteFormat: doubleDigitMinuteFormat,
singleDigitSecondFormat: singleDigitSecondFormat,
decimalFormat: decimalFormat,
);''';
};
const String cupertinoFactoryName = 'getCupertinoTranslation';
const String cupertinoFactoryDeclaration = '''
GlobalCupertinoLocalizations getCupertinoTranslation(
Locale locale,
intl.DateFormat fullYearFormat,
intl.DateFormat dayFormat,
intl.DateFormat mediumDateFormat,
intl.DateFormat singleDigitHourFormat,
intl.DateFormat singleDigitMinuteFormat,
intl.DateFormat doubleDigitMinuteFormat,
intl.DateFormat singleDigitSecondFormat,
intl.NumberFormat decimalFormat,
) {''';
const String cupertinoFactoryArguments =
'fullYearFormat: fullYearFormat, dayFormat: dayFormat, mediumDateFormat: mediumDateFormat, singleDigitHourFormat: singleDigitHourFormat, singleDigitMinuteFormat: singleDigitMinuteFormat, doubleDigitMinuteFormat: doubleDigitMinuteFormat, singleDigitSecondFormat: singleDigitSecondFormat, decimalFormat: decimalFormat';
const String cupertinoSupportedLanguagesConstant = 'kCupertinoSupportedLanguages';
const String cupertinoSupportedLanguagesDocMacro = 'flutter.localizations.cupertino.languages';

View File

@@ -2,10 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This program generates a getMaterialTranslation() function that looks up the
// translations provided by the arb files. The returned value is a generated
// instance of GlobalMaterialLocalizations that corresponds to a single
// locale.
// This program generates a getMaterialTranslation() and a
// getCupertinoTranslation() function that look up the translations provided by
// the arb files. The returned value is a generated instance of a
// GlobalMaterialLocalizations or a GlobalCupertinoLocalizations that
// corresponds to a single locale.
//
// The *.arb files are in packages/flutter_localizations/lib/src/l10n.
//
@@ -13,10 +14,10 @@
// Each map value is itself a map with resource identifier keys and localized
// resource string values.
//
// The arb filenames are expected to have the form "material_(\w+)\.arb", where
// the group following "_" identifies the language code and the country code,
// e.g. "material_en.arb" or "material_en_GB.arb". In most cases both codes are
// just two characters.
// The arb filenames are expected to have the form "material_(\w+)\.arb" or
// "cupertino_(\w+)\.arb" where the group following "_" identifies the language
// code and the country code, e.g. "material_en.arb" or "material_en_GB.arb".
// In most cases both codes are just two characters.
//
// This app is typically run by hand when a module's .arb files have been
// updated.
@@ -32,7 +33,8 @@
// ```
//
// If the data looks good, use the `-w` or `--overwrite` option to overwrite the
// packages/flutter_localizations/lib/src/l10n/generated_material_localizations.dart file:
// packages/flutter_localizations/lib/src/l10n/generated_material_localizations.dart
// and packages/flutter_localizations/lib/src/l10n/generated_cupertino_localizations.dart file:
//
// ```
// dart dev/tools/localization/gen_localizations.dart --overwrite
@@ -44,6 +46,7 @@ import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:meta/meta.dart';
import 'gen_cupertino_localizations.dart';
import 'gen_material_localizations.dart';
import 'localizations_utils.dart';
import 'localizations_validator.dart';
@@ -56,8 +59,11 @@ String generateArbBasedLocalizationSubclasses({
@required String baseClass,
@required HeaderGenerator generateHeader,
@required ConstructorGenerator generateConstructor,
@required String factoryName,
@required String factoryDeclaration,
@required String factoryArguments,
@required String supportedLanguagesConstant,
@required String supportedLanguagesDocMacro,
}) {
assert(localeToResources != null);
assert(localeToResourceAttributes != null);
@@ -65,8 +71,11 @@ String generateArbBasedLocalizationSubclasses({
assert(baseClass.isNotEmpty);
assert(generateHeader != null);
assert(generateConstructor != null);
assert(factoryName.isNotEmpty);
assert(factoryDeclaration.isNotEmpty);
assert(factoryArguments.isNotEmpty);
assert(supportedLanguagesConstant.isNotEmpty);
assert(supportedLanguagesDocMacro.isNotEmpty);
final StringBuffer output = StringBuffer();
output.writeln(generateHeader('dart dev/tools/localization/gen_localizations.dart --overwrite'));
@@ -232,9 +241,9 @@ String generateArbBasedLocalizationSubclasses({
///
/// See also:
///
/// * [getMaterialTranslation], whose documentation describes these values.
final Set<String> kSupportedLanguages = HashSet<String>.from(const <String>[
${languageCodes.map<String>((String value) => " '$value', // ${describeLocale(value)}").join('\n')}
/// * [$factoryName], whose documentation describes these values.
final Set<String> $supportedLanguagesConstant = HashSet<String>.from(const <String>[
${languageCodes.map<String>((String value) => " '$value', // ${describeLocale(value)}").toList().join('\n')}
]);
/// Creates a [$baseClass] instance for the given `locale`.
@@ -246,7 +255,7 @@ ${languageCodes.map<String>((String value) => " '$value', // ${describeLocale(v
///
/// The following locales are supported by this package:
///
/// {@template flutter.localizations.languages}
/// {@template $supportedLanguagesDocMacro}
$supportedLocales/// {@endtemplate}
///
/// Generally speaking, this method is only intended to be used by
@@ -357,7 +366,7 @@ $factoryDeclaration
}
output.writeln('''
}
assert(false, 'getMaterialTranslation() called for unsupported locale "\$locale"');
assert(false, '$factoryName() called for unsupported locale "\$locale"');
return null;
}''');
@@ -397,6 +406,10 @@ String generateKey(String key, Map<String, dynamic> attributes) {
return '${key}Raw';
}
}
if (key == 'datePickerDateOrder')
return 'datePickerDateOrderString';
if (key == 'datePickerDateTimeOrder')
return 'datePickerDateTimeOrderString';
return key;
}
@@ -518,21 +531,48 @@ Future<void> main(List<String> rawArgs) async {
exitWithError('$exception');
}
final String materialLocalizations = generateArbBasedLocalizationSubclasses(
localeToResources: materialLocaleToResources,
localeToResourceAttributes: materialLocaleToResourceAttributes,
generatedClassPrefix: 'MaterialLocalization',
baseClass: 'GlobalMaterialLocalizations',
generateHeader: generateMaterialHeader,
generateConstructor: generateMaterialConstructor,
factoryDeclaration: materialFactoryDeclaration,
factoryArguments: materialFactoryArguments,
);
final String materialLocalizations = options.writeToFile || !options.cupertinoOnly
? generateArbBasedLocalizationSubclasses(
localeToResources: materialLocaleToResources,
localeToResourceAttributes: materialLocaleToResourceAttributes,
generatedClassPrefix: 'MaterialLocalization',
baseClass: 'GlobalMaterialLocalizations',
generateHeader: generateMaterialHeader,
generateConstructor: generateMaterialConstructor,
factoryName: materialFactoryName,
factoryDeclaration: materialFactoryDeclaration,
factoryArguments: materialFactoryArguments,
supportedLanguagesConstant: materialSupportedLanguagesConstant,
supportedLanguagesDocMacro: materialSupportedLanguagesDocMacro,
)
: null;
final String cupertinoLocalizations = options.writeToFile || !options.materialOnly
? generateArbBasedLocalizationSubclasses(
localeToResources: cupertinoLocaleToResources,
localeToResourceAttributes: cupertinoLocaleToResourceAttributes,
generatedClassPrefix: 'CupertinoLocalization',
baseClass: 'GlobalCupertinoLocalizations',
generateHeader: generateCupertinoHeader,
generateConstructor: generateCupertinoConstructor,
factoryName: cupertinoFactoryName,
factoryDeclaration: cupertinoFactoryDeclaration,
factoryArguments: cupertinoFactoryArguments,
supportedLanguagesConstant: cupertinoSupportedLanguagesConstant,
supportedLanguagesDocMacro: cupertinoSupportedLanguagesDocMacro,
)
: null;
if (options.writeToFile) {
final File localizationsFile = File(path.join(directory.path, 'generated_material_localizations.dart'));
localizationsFile.writeAsStringSync(materialLocalizations, flush: true);
final File materialLocalizationsFile = File(path.join(directory.path, 'generated_material_localizations.dart'));
materialLocalizationsFile.writeAsStringSync(materialLocalizations, flush: true);
final File cupertinoLocalizationsFile = File(path.join(directory.path, 'generated_cupertino_localizations.dart'));
cupertinoLocalizationsFile.writeAsStringSync(cupertinoLocalizations, flush: true);
} else {
stdout.write(materialLocalizations);
if (!options.cupertinoOnly) {
stdout.write(materialLocalizations);
}
if (!options.materialOnly) {
stdout.write(cupertinoLocalizations);
}
}
}

View File

@@ -53,6 +53,8 @@ ConstructorGenerator generateMaterialConstructor = (LocaleInfo locale) {
);''';
};
const String materialFactoryName = 'getMaterialTranslation';
const String materialFactoryDeclaration = '''
GlobalMaterialLocalizations getMaterialTranslation(
Locale locale,
@@ -66,3 +68,7 @@ GlobalMaterialLocalizations getMaterialTranslation(
const String materialFactoryArguments =
'fullYearFormat: fullYearFormat, mediumDateFormat: mediumDateFormat, longDateFormat: longDateFormat, yearMonthFormat: yearMonthFormat, decimalFormat: decimalFormat, twoDigitZeroPaddedFormat: twoDigitZeroPaddedFormat';
const String materialSupportedLanguagesConstant = 'kMaterialSupportedLanguages';
const String materialSupportedLanguagesDocMacro = 'flutter.localizations.material.languages';

View File

@@ -231,19 +231,35 @@ GeneratorOptions parseArgs(List<String> rawArgs) {
'overwrite',
abbr: 'w',
defaultsTo: false,
)
..addFlag(
'material',
help: 'Whether to print the generated classes for the Material package only. Ignored when --overwrite is passed.',
defaultsTo: false,
)
..addFlag(
'cupertino',
help: 'Whether to print the generated classes for the Cupertino package only. Ignored when --overwrite is passed.',
defaultsTo: false,
);
final argslib.ArgResults args = argParser.parse(rawArgs);
final bool writeToFile = args['overwrite'];
final bool materialOnly = args['material'];
final bool cupertinoOnly = args['cupertino'];
return GeneratorOptions(writeToFile: writeToFile);
return GeneratorOptions(writeToFile: writeToFile, materialOnly: materialOnly, cupertinoOnly: cupertinoOnly);
}
class GeneratorOptions {
GeneratorOptions({
@required this.writeToFile,
@required this.materialOnly,
@required this.cupertinoOnly,
});
final bool writeToFile;
final bool materialOnly;
final bool cupertinoOnly;
}
const String registry = 'https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry';