Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Language-specific LocalizationDelegates #12645

Merged
merged 3 commits into from
Oct 20, 2017

Conversation

HansMuller
Copy link
Contributor

Make it easier to add language-specific versions of MaterialLocalizations.

Apps can define their own implementations of MaterialLocalizations that support languages which are not supported by the flutter_localizations package.

When a Flutter app starts or switches locales it will now select the first LocalizationDelegate for each LocalizationDelegate.type that supports the new locale. LocalizationDelegates must override their isSupported(Locale) method.

So to add material library support for a new language an app would define a subclass of MaterialLocalizations, say MyPolishMaterialLocalizations. The new Polish localizations would be loaded by a LocalizationsDelegate defined like this:

class PolishLocalizationsDelegate extends LocalizationsDelegate<MaterialLocalizations> {
  const PolishLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) => locale.languageCode == 'pl';

  @override
  Future<MaterialLocalizations> load(Locale locale) => MyPolishMaterialLocalizations.load(locale);

  @override
  bool shouldReload(PolishLocalizationsDelegate old) => false;
}

A Material app that included the new Polish localizations would be created like this:

new MaterialApp(
  localizationsDelegates: [
    const PolishLocalizationsDelegate(),
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: [
    const Locale('pl', 'PL'),
    // .. the other supported locales here
  ]
)

@@ -188,6 +188,9 @@ class _MaterialLocalizationsDelegate extends LocalizationsDelegate<MaterialLocal
const _MaterialLocalizationsDelegate();

@override
bool isSupported(Locale locale) => locale.languageCode == 'en';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only en-US, right?

final Set<Type> types = new Set<Type>();
final List<LocalizationsDelegate<dynamic>> delegates = <LocalizationsDelegate<dynamic>>[];
for (LocalizationsDelegate<dynamic> delegate in allDelegates) {
if (!types.contains(delegate.type)) {
if (delegate.isSupported(locale) && !types.contains(delegate.type)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would flip the order in which you make these calls, so that we don't contact the delegate if we don't need to.

@@ -97,6 +99,12 @@ abstract class LocalizationsDelegate<T> {
/// const constructors so that they can be used in const expressions.
const LocalizationsDelegate();

/// Return true if the instance of 'T' loaded by this delegate's [load]
/// method supports the given `locale`'s language.
bool isSupported(Locale locale) => true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe prefix with something like:

/// Whether resources for the given locale can be loaded by this delegate.
///

Also either use backticks or no quotes at all around T. Single quotes makes it look like a string.

/// method supports the given `locale`'s language.
bool isSupported(Locale locale) => true;
// TODO(hansmuller): remove the default implementation after existing
// apps have had a chance to update.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless you mean that you plan to remove it in a couple of days, i would remove it now. the longer we leave this the more likely it is we'll have people depending on it.

// This is convenient simplification. It would be more correct test if locale's
// text-direction is LTR.
@override
bool isSupported(Locale locale) => true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this'll get resolved once we have the concrete classes, right?

@@ -68,6 +68,9 @@ class _WidgetsLocalizationsDelegate extends LocalizationsDelegate<WidgetsLocaliz
const _WidgetsLocalizationsDelegate();

@override
bool isSupported(Locale locale) => true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only en-US, right?

@Hixie
Copy link
Contributor

Hixie commented Oct 20, 2017

LGTM

@HansMuller HansMuller force-pushed the localization_delegate_supports branch from 718cc44 to 0bc7771 Compare October 20, 2017 00:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants