From 796c5439f6d14d1d35671fc39bacb8233590f91e Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Fri, 20 Oct 2017 10:45:28 -0700 Subject: [PATCH] Language-specific LocalizationDelegates (#12645) --- examples/stocks/lib/main.dart | 3 + .../src/material/material_localizations.dart | 3 + .../lib/src/widgets/localizations.dart | 16 +++- .../test/material/text_field_test.dart | 6 ++ .../lib/src/material_localizations.dart | 21 +++++ .../lib/src/widgets_localizations.dart | 3 + .../test/basics_test.dart | 3 + .../test/override_test.dart | 90 +++++++++++++++++-- .../test/widgets_test.dart | 15 ++++ 9 files changed, 152 insertions(+), 8 deletions(-) diff --git a/examples/stocks/lib/main.dart b/examples/stocks/lib/main.dart index a89769ab76..5cef335096 100644 --- a/examples/stocks/lib/main.dart +++ b/examples/stocks/lib/main.dart @@ -26,6 +26,9 @@ class _StocksLocalizationsDelegate extends LocalizationsDelegate { @override Future load(Locale locale) => StockStrings.load(locale); + @override + bool isSupported(Locale locale) => locale.languageCode == 'es' || locale.languageCode == 'en'; + @override bool shouldReload(_StocksLocalizationsDelegate old) => false; } diff --git a/packages/flutter/lib/src/material/material_localizations.dart b/packages/flutter/lib/src/material/material_localizations.dart index c872dbb99e..a06e245c4a 100644 --- a/packages/flutter/lib/src/material/material_localizations.dart +++ b/packages/flutter/lib/src/material/material_localizations.dart @@ -187,6 +187,9 @@ abstract class MaterialLocalizations { class _MaterialLocalizationsDelegate extends LocalizationsDelegate { const _MaterialLocalizationsDelegate(); + @override + bool isSupported(Locale locale) => locale.languageCode == 'en'; + @override Future load(Locale locale) => DefaultMaterialLocalizations.load(locale); diff --git a/packages/flutter/lib/src/widgets/localizations.dart b/packages/flutter/lib/src/widgets/localizations.dart index ace2e3be70..1335fd63a2 100644 --- a/packages/flutter/lib/src/widgets/localizations.dart +++ b/packages/flutter/lib/src/widgets/localizations.dart @@ -42,11 +42,12 @@ Future> _loadAll(Locale locale, Iterable output = {}; List<_Pending> pendingList; - // Only load the first delegate for each delgate type. + // Only load the first delegate for each delegate type that supports + // locale.languageCode. final Set types = new Set(); final List> delegates = >[]; for (LocalizationsDelegate delegate in allDelegates) { - if (!types.contains(delegate.type)) { + if (!types.contains(delegate.type) && delegate.isSupported(locale)) { types.add(delegate.type); delegates.add(delegate); } @@ -97,6 +98,12 @@ abstract class LocalizationsDelegate { /// const constructors so that they can be used in const expressions. const LocalizationsDelegate(); + /// Whether resources for the given locale can be loaded by this delegate. + /// + /// Return true if the instance of `T` loaded by this delegate's [load] + /// method supports the given `locale`'s language. + bool isSupported(Locale locale); + /// Start loading the resources for `locale`. The returned future completes /// when the resources have finished loading. /// @@ -164,6 +171,11 @@ abstract class WidgetsLocalizations { class _WidgetsLocalizationsDelegate extends LocalizationsDelegate { const _WidgetsLocalizationsDelegate(); + // This is convenient simplification. It would be more correct test if the locale's + // text-direction is LTR. + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => DefaultWidgetsLocalizations.load(locale); diff --git a/packages/flutter/test/material/text_field_test.dart b/packages/flutter/test/material/text_field_test.dart index fdd041d8f4..abed7b36ba 100644 --- a/packages/flutter/test/material/text_field_test.dart +++ b/packages/flutter/test/material/text_field_test.dart @@ -28,6 +28,9 @@ class MockClipboard { } class MaterialLocalizationsDelegate extends LocalizationsDelegate { + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => DefaultMaterialLocalizations.load(locale); @@ -36,6 +39,9 @@ class MaterialLocalizationsDelegate extends LocalizationsDelegate { + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => DefaultWidgetsLocalizations.load(locale); diff --git a/packages/flutter_localizations/lib/src/material_localizations.dart b/packages/flutter_localizations/lib/src/material_localizations.dart index a48dd7a8a8..891bae277c 100644 --- a/packages/flutter_localizations/lib/src/material_localizations.dart +++ b/packages/flutter_localizations/lib/src/material_localizations.dart @@ -423,6 +423,27 @@ void _loadDateIntlDataIfNotLoaded() { class _MaterialLocalizationsDelegate extends LocalizationsDelegate { const _MaterialLocalizationsDelegate(); + static const List _supportedLanguages = const [ + 'ar', // Arabic + 'de', // German + 'en', // English + 'es', // Spanish + 'fa', // Farsi + 'fr', // French + 'he', // Hebrew + 'it', // Italian + 'ja', // Japanese + 'ps', // Pashto + 'pt', // Portugese + 'ru', // Russian + 'sd', // Sindhi + 'ur', // Urdu + 'zh', // Simplified Chinese + ]; + + @override + bool isSupported(Locale locale) => _supportedLanguages.contains(locale.languageCode); + @override Future load(Locale locale) => GlobalMaterialLocalizations.load(locale); diff --git a/packages/flutter_localizations/lib/src/widgets_localizations.dart b/packages/flutter_localizations/lib/src/widgets_localizations.dart index 9670af2a56..9655bc5288 100644 --- a/packages/flutter_localizations/lib/src/widgets_localizations.dart +++ b/packages/flutter_localizations/lib/src/widgets_localizations.dart @@ -67,6 +67,9 @@ class GlobalWidgetsLocalizations implements WidgetsLocalizations { class _WidgetsLocalizationsDelegate extends LocalizationsDelegate { const _WidgetsLocalizationsDelegate(); + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => GlobalWidgetsLocalizations.load(locale); diff --git a/packages/flutter_localizations/test/basics_test.dart b/packages/flutter_localizations/test/basics_test.dart index f6f7883c02..52d3c2b485 100644 --- a/packages/flutter_localizations/test/basics_test.dart +++ b/packages/flutter_localizations/test/basics_test.dart @@ -55,6 +55,9 @@ class _DummyLocalizationsDelegate extends LocalizationsDelegate load(Locale locale) async => new DummyLocalizations(); + @override + bool isSupported(Locale locale) => true; + @override bool shouldReload(_DummyLocalizationsDelegate old) => true; } diff --git a/packages/flutter_localizations/test/override_test.dart b/packages/flutter_localizations/test/override_test.dart index 16cc58d69a..8406492bfc 100644 --- a/packages/flutter_localizations/test/override_test.dart +++ b/packages/flutter_localizations/test/override_test.dart @@ -8,18 +8,29 @@ import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_test/flutter_test.dart'; class FooMaterialLocalizations extends GlobalMaterialLocalizations { - FooMaterialLocalizations(Locale locale) : super(locale); + FooMaterialLocalizations(Locale locale, this.backButtonTooltip) : super(locale); @override - String get backButtonTooltip => 'foo'; + final String backButtonTooltip; } class FooMaterialLocalizationsDelegate extends LocalizationsDelegate { - const FooMaterialLocalizationsDelegate(); + const FooMaterialLocalizationsDelegate({ + this.supportedLanguage: 'en', + this.backButtonTooltip: 'foo' + }); + + final String supportedLanguage; + final String backButtonTooltip; + + @override + bool isSupported(Locale locale) { + return supportedLanguage == 'allLanguages' ? true : locale.languageCode == supportedLanguage; + } @override Future load(Locale locale) { - return new SynchronousFuture(new FooMaterialLocalizations(locale)); + return new SynchronousFuture(new FooMaterialLocalizations(locale, backButtonTooltip)); } @override @@ -143,7 +154,42 @@ void main() { expect(find.text('Back'), findsOneWidget); }); - testWidgets('MaterialApp overrides MaterialLocalizations', (WidgetTester tester) async { + testWidgets('MaterialApp adds MaterialLocalizations for additional languages', (WidgetTester tester) async { + final Key textKey = new UniqueKey(); + + await tester.pumpWidget( + buildFrame( + delegates: [ + const FooMaterialLocalizationsDelegate(supportedLanguage: 'fr', backButtonTooltip: 'FR'), + const FooMaterialLocalizationsDelegate(supportedLanguage: 'de', backButtonTooltip: 'DE'), + ], + supportedLocales: const [ + const Locale('en', ''), + const Locale('fr', ''), + const Locale('de', ''), + ], + buildContent: (BuildContext context) { + // Should always be 'foo', no matter what the locale is + return new Text( + MaterialLocalizations.of(context).backButtonTooltip, + key: textKey, + ); + } + ) + ); + + expect(tester.widget(find.byKey(textKey)).data, 'Back'); + + await tester.binding.setLocale('fr', 'CA'); + await tester.pump(); + expect(find.text('FR'), findsOneWidget); + + await tester.binding.setLocale('de', 'DE'); + await tester.pump(); + expect(find.text('DE'), findsOneWidget); + }); + + testWidgets('MaterialApp overrides MaterialLocalizations for all locales', (WidgetTester tester) async { final Key textKey = new UniqueKey(); await tester.pumpWidget( @@ -151,7 +197,7 @@ void main() { // Accept whatever locale we're given localeResolutionCallback: (Locale locale, Iterable supportedLocales) => locale, delegates: [ - const FooMaterialLocalizationsDelegate(), + const FooMaterialLocalizationsDelegate(supportedLanguage: 'allLanguages'), ], buildContent: (BuildContext context) { // Should always be 'foo', no matter what the locale is @@ -174,6 +220,38 @@ void main() { expect(find.text('foo'), findsOneWidget); }); + testWidgets('MaterialApp overrides MaterialLocalizations for default locale', (WidgetTester tester) async { + final Key textKey = new UniqueKey(); + + await tester.pumpWidget( + buildFrame( + delegates: [ + const FooMaterialLocalizationsDelegate(supportedLanguage: 'en'), + ], + // supportedLocales not specified, so all locales resolve to 'en' + buildContent: (BuildContext context) { + return new Text( + MaterialLocalizations.of(context).backButtonTooltip, + key: textKey, + ); + } + ) + ); + + // Unsupported locale '_' (the widget tester's default) resolves to 'en'. + expect(tester.widget(find.byKey(textKey)).data, 'foo'); + + // Unsupported locale 'zh' resolves to 'en'. + await tester.binding.setLocale('zh', 'CN'); + await tester.pump(); + expect(find.text('foo'), findsOneWidget); + + // Unsupported locale 'de' resolves to 'en'. + await tester.binding.setLocale('de', 'DE'); + await tester.pump(); + expect(find.text('foo'), findsOneWidget); + }); + testWidgets('deprecated Android/Java locales are modernized', (WidgetTester tester) async { final Key textKey = new UniqueKey(); diff --git a/packages/flutter_localizations/test/widgets_test.dart b/packages/flutter_localizations/test/widgets_test.dart index e5ca6280f0..a1e955f31c 100644 --- a/packages/flutter_localizations/test/widgets_test.dart +++ b/packages/flutter_localizations/test/widgets_test.dart @@ -35,6 +35,9 @@ class SyncTestLocalizationsDelegate extends LocalizationsDelegate shouldReloadValues = []; + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => TestLocalizations.loadSync(locale, prefix); @@ -54,6 +57,9 @@ class AsyncTestLocalizationsDelegate extends LocalizationsDelegate shouldReloadValues = []; + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) => TestLocalizations.loadAsync(locale, prefix); @@ -92,6 +98,9 @@ class SyncMoreLocalizationsDelegate extends LocalizationsDelegate load(Locale locale) => MoreLocalizations.loadSync(locale); + @override + bool isSupported(Locale locale) => true; + @override bool shouldReload(SyncMoreLocalizationsDelegate old) => false; } @@ -100,6 +109,9 @@ class AsyncMoreLocalizationsDelegate extends LocalizationsDelegate load(Locale locale) => MoreLocalizations.loadAsync(locale); + @override + bool isSupported(Locale locale) => true; + @override bool shouldReload(AsyncMoreLocalizationsDelegate old) => false; } @@ -112,6 +124,9 @@ class OnlyRTLDefaultWidgetsLocalizations extends DefaultWidgetsLocalizations { class OnlyRTLDefaultWidgetsLocalizationsDelegate extends LocalizationsDelegate { const OnlyRTLDefaultWidgetsLocalizationsDelegate(); + @override + bool isSupported(Locale locale) => true; + @override Future load(Locale locale) { return new SynchronousFuture(new OnlyRTLDefaultWidgetsLocalizations());