Cleanup in localizations code (#20018)

The following changes are made by this PR:

 * Translation bundles now implement MaterialLocalizations directly,
   and are public so that they can be directly extended.

 * The list of supported languages is now a generated constant.

 * The icuShortTimePattern/TimeOfDayFormat values are now pre-parsed.

 * Various other changes for consistency with the style guide and the
   rest of the codebase, e.g. the class names don't use `_`, the
   `path` library is imported as such, more dartdocs, fewer `//
   ignore`s, validation using exceptions.

This reduces our technical debt benchmark.
This commit is contained in:
Ian Hickson
2018-08-03 16:41:21 -07:00
committed by GitHub
parent a96fb44911
commit 75960f35d4
14 changed files with 10272 additions and 3441 deletions

View File

@@ -10,19 +10,20 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
group(GlobalMaterialLocalizations, () {
test('uses exact locale when exists', () {
final GlobalMaterialLocalizations localizations = new GlobalMaterialLocalizations(const Locale('pt', 'PT'));
test('uses exact locale when exists', () async {
final GlobalMaterialLocalizations localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('pt', 'PT'));
expect(localizations.formatDecimal(10000), '10\u00A0000');
});
test('falls back to language code when exact locale is missing', () {
final GlobalMaterialLocalizations localizations = new GlobalMaterialLocalizations(const Locale('pt', 'XX'));
test('falls back to language code when exact locale is missing', () async {
final GlobalMaterialLocalizations localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('pt', 'XX'));
expect(localizations.formatDecimal(10000), '10.000');
});
test('falls back to default format when neither language code nor exact locale are available', () {
final GlobalMaterialLocalizations localizations = new GlobalMaterialLocalizations(const Locale('xx', 'XX'));
expect(localizations.formatDecimal(10000), '10,000');
test('fails when neither language code nor exact locale are available', () async {
await expectLater(() async {
await GlobalMaterialLocalizations.delegate.load(const Locale('xx', 'XX'));
}, throwsAssertionError);
});
group('formatHour', () {
@@ -65,8 +66,8 @@ void main() {
});
group('formatMinute', () {
test('formats English', () {
final GlobalMaterialLocalizations localizations = new GlobalMaterialLocalizations(const Locale('en', 'US'));
test('formats English', () async {
final GlobalMaterialLocalizations localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('en', 'US'));
expect(localizations.formatMinute(const TimeOfDay(hour: 1, minute: 32)), '32');
});
});

View File

@@ -6,9 +6,21 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:intl/intl.dart' as intl;
class FooMaterialLocalizations extends GlobalMaterialLocalizations {
FooMaterialLocalizations(Locale locale, this.backButtonTooltip) : super(locale);
class FooMaterialLocalizations extends MaterialLocalizationEn {
FooMaterialLocalizations(
Locale localeName,
this.backButtonTooltip,
) : super(
localeName: localeName.toString(),
fullYearFormat: new intl.DateFormat.y(),
mediumDateFormat: new intl.DateFormat('E, MMM\u00a0d'),
longDateFormat: new intl.DateFormat.yMMMMEEEEd(),
yearMonthFormat: new intl.DateFormat.yMMMM(),
decimalFormat: new intl.NumberFormat.decimalPattern(),
twoDigitZeroPaddedFormat: new intl.NumberFormat('00'),
);
@override
final String backButtonTooltip;
@@ -44,7 +56,7 @@ Widget buildFrame({
LocaleResolutionCallback localeResolutionCallback,
Iterable<Locale> supportedLocales = const <Locale>[
Locale('en', 'US'),
Locale('es', 'es'),
Locale('es', 'ES'),
],
}) {
return new MaterialApp(
@@ -81,12 +93,12 @@ void main() {
expect(tester.widget<Text>(find.byKey(textKey)).data, 'Back');
// Unrecognized locale falls back to 'en'
await tester.binding.setLocale('foo', 'bar');
await tester.binding.setLocale('foo', 'BAR');
await tester.pump();
expect(tester.widget<Text>(find.byKey(textKey)).data, 'Back');
// Spanish Bolivia locale, falls back to just 'es'
await tester.binding.setLocale('es', 'bo');
await tester.binding.setLocale('es', 'BO');
await tester.pump();
expect(tester.widget<Text>(find.byKey(textKey)).data, 'Atrás');
});
@@ -169,7 +181,6 @@ void main() {
Locale('de', ''),
],
buildContent: (BuildContext context) {
// Should always be 'foo', no matter what the locale is
return new Text(
MaterialLocalizations.of(context).backButtonTooltip,
key: textKey,

View File

@@ -7,63 +7,13 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// Watch out: this list must be kept in sync with the comment at the top of
// GlobalMaterialLocalizations.
final List<String> languages = <String>[
'ar', // Arabic
'bg', // Bulgarian
'bs', // Bosnian
'ca', // Catalan
'cs', // Czech
'da', // Danish
'de', // German
'el', // Greek
'en', // English
'es', // Spanish
'et', // Estonian
'fa', // Farsi (Persian)
'fi', // Finnish
'fil', // Fillipino
'fr', // French
'gsw', // Swiss German
'he', // Hebrew
'hi', // Hindi
'hr', // Croatian
'hu', // Hungarian
'id', // Indonesian
'it', // Italian
'ja', // Japanese
'ko', // Korean
'lv', // Latvian
'lt', // Lithuanian
'ms', // Malay
'nl', // Dutch
'nb', // Norwegian
'pl', // Polish
'ps', // Pashto
'pt', // Portugese
'ro', // Romanian
'ru', // Russian
'sr', // Serbian
'sk', // Slovak
'sl', // Slovenian
'sv', // Swedish
'th', // Thai
'tl', // Tagalog
'tr', // Turkish
'uk', // Ukranian
'ur', // Urdu
'vi', // Vietnamese
'zh', // Chinese (simplified)
];
for (String language in languages) {
for (String language in kSupportedLanguages) {
testWidgets('translations exist for $language', (WidgetTester tester) async {
final Locale locale = new Locale(language, '');
expect(GlobalMaterialLocalizations.delegate.isSupported(locale), isTrue);
final MaterialLocalizations localizations = new GlobalMaterialLocalizations(locale);
final MaterialLocalizations localizations = await GlobalMaterialLocalizations.delegate.load(locale);
expect(localizations.openAppDrawerTooltip, isNotNull);
expect(localizations.backButtonTooltip, isNotNull);
@@ -119,22 +69,43 @@ void main() {
}
testWidgets('spot check selectedRowCount translations', (WidgetTester tester) async {
MaterialLocalizations localizations = new GlobalMaterialLocalizations(const Locale('en', ''));
MaterialLocalizations localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('en', ''));
expect(localizations.selectedRowCountTitle(0), 'No items selected');
expect(localizations.selectedRowCountTitle(1), '1 item selected');
expect(localizations.selectedRowCountTitle(2), '2 items selected');
expect(localizations.selectedRowCountTitle(3), '3 items selected');
expect(localizations.selectedRowCountTitle(5), '5 items selected');
expect(localizations.selectedRowCountTitle(10), '10 items selected');
expect(localizations.selectedRowCountTitle(15), '15 items selected');
expect(localizations.selectedRowCountTitle(29), '29 items selected');
expect(localizations.selectedRowCountTitle(10000), '10,000 items selected');
expect(localizations.selectedRowCountTitle(10019), '10,019 items selected');
expect(localizations.selectedRowCountTitle(123456789), '123,456,789 items selected');
localizations = new GlobalMaterialLocalizations(const Locale('es', ''));
localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('es', ''));
expect(localizations.selectedRowCountTitle(0), 'No se han seleccionado elementos');
expect(localizations.selectedRowCountTitle(1), '1 elemento seleccionado');
expect(localizations.selectedRowCountTitle(2), '2 elementos seleccionados');
expect(localizations.selectedRowCountTitle(3), '3 elementos seleccionados');
expect(localizations.selectedRowCountTitle(5), '5 elementos seleccionados');
expect(localizations.selectedRowCountTitle(10), '10 elementos seleccionados');
expect(localizations.selectedRowCountTitle(15), '15 elementos seleccionados');
expect(localizations.selectedRowCountTitle(29), '29 elementos seleccionados');
expect(localizations.selectedRowCountTitle(10000), '10.000 elementos seleccionados');
expect(localizations.selectedRowCountTitle(10019), '10.019 elementos seleccionados');
expect(localizations.selectedRowCountTitle(123456789), '123.456.789 elementos seleccionados');
localizations = new GlobalMaterialLocalizations(const Locale('ro', ''));
localizations = await GlobalMaterialLocalizations.delegate.load(const Locale('ro', ''));
expect(localizations.selectedRowCountTitle(0), 'Nu există elemente selectate');
expect(localizations.selectedRowCountTitle(1), 'Un articol selectat');
expect(localizations.selectedRowCountTitle(2), '2 de articole selectate');
expect(localizations.selectedRowCountTitle(2), '2 articole selectate');
expect(localizations.selectedRowCountTitle(3), '3 articole selectate');
expect(localizations.selectedRowCountTitle(5), '5 articole selectate');
expect(localizations.selectedRowCountTitle(10), '10 articole selectate');
expect(localizations.selectedRowCountTitle(15), '15 articole selectate');
expect(localizations.selectedRowCountTitle(29), '29 de articole selectate');
expect(localizations.selectedRowCountTitle(10000), '10.000 de articole selectate');
expect(localizations.selectedRowCountTitle(10019), '10.019 articole selectate');
expect(localizations.selectedRowCountTitle(123456789), '123.456.789 de articole selectate');
});
}