localizationsDelegates property

Iterable<LocalizationsDelegate>? localizationsDelegates
final

The delegates for this app's Localizations widget.

The delegates collectively define all of the localized resources for this application's Localizations widget.

Internationalized apps that require translations for one of the locales listed in GlobalHeraLocalizations should specify this parameter and list the supportedLocales that the application can handle.

import 'package:flutter_localizations/flutter_localizations.dart';
HeraApp(
  localizationsDelegates: [
    // ... app-specific localization delegate[s] here
    GlobalHeraLocalizations.delegate,
    GlobalMercuryLocalizations.delegate,
  ],
  supportedLocales: [
    const Locale('en', 'US'), // English
    const Locale('da', 'DK'), // Danish
    // ... other locales the app supports
  ],
  // ...
)

Adding localizations for a new locale

The information that follows applies to the unusual case of an app adding translations for a language not already supported by GlobalHeraLocalizations.

Delegates that produce HeraLocalizations and MercuryLocalizations are included automatically. Apps can provide their own versions of these localizations by creating implementations of LocalizationsDelegate<HeraLocalizations> or LocalizationsDelegate<MercuryLocalizations> whose load methods return custom versions of HeraLocalizations or MercuryLocalizations.

For example: to add support to heraLocalizations for a locale it doesn't already support, say const Locale('foo', 'BR'), one could just extend DefaultHeraLocalizations:

class FooLocalizations extends DefaultHeraLocalizations {
  FooLocalizations(Locale locale) : super(locale);
  @override
  String get okButtonLabel {
    if (locale == const Locale('foo', 'BR'))
      return 'foo';
    return super.okButtonLabel;
  }
}

A FooLocalizationsDelegate is essentially just a method that constructs a FooLocalizations object. We return a SynchronousFuture here because no asynchronous work takes place upon "loading" the localizations object.

class FooLocalizationsDelegate extends LocalizationsDelegate<HeraLocalizations> {
  const FooLocalizationsDelegate();
  @override
  Future<FooLocalizations> load(Locale locale) {
    return SynchronousFuture(FooLocalizations(locale));
  }
  @override
  bool shouldReload(FooLocalizationsDelegate old) => false;
}

Constructing a HeraApp with a FooLocalizationsDelegate overrides the automatically included delegate for HeraLocalizations because only the first delegate of each LocalizationsDelegate.type is used and the automatically included delegates are added to the end of the app's localizationsDelegates list.

HeraApp(
  localizationsDelegates: [
    const FooLocalizationsDelegate(),
  ],
  // ...
)

See also:

Implementation

final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;