load<T> static method

Future<T> load<T>(
  1. InitializeMessages initializeMessages,
  2. Locale locale,
  3. FutureOr<T> builder(
    1. String locale
    ), {
  4. bool setDefaultLocale = false,
  5. String? fallbackLocale,
})

Load messages for localization and create localization instance.

Use setDefaultLocale to set loaded locale as Intl.defaultLocale.

Use fallbackLocale to set locale which will be used if some key not found for current locale. Only first call of load will set fallback locale, make sure that your app's localization delegate added as a first element of the delegates list. Also pay attention that if you provide the fallbackLocale, than all messages will be uploaded in memory on the start of application in addition to current locale.

Implementation

static Future<T> load<T>(InitializeMessages initializeMessages, Locale locale,
    FutureOr<T> Function(String locale) builder,
    {bool setDefaultLocale = false, String? fallbackLocale}) async {
  final lookup = _lookup ??= _init(fallbackLocale != null
      ? Intl.canonicalizedLocale(fallbackLocale)
      : null);
  final name = locale.toString();
  final localeName = Intl.canonicalizedLocale(name);

  final res = await initializeMessages(localeName).then((_) {
    if (setDefaultLocale) {
      Intl.defaultLocale = localeName;
    }

    return builder(localeName);
  });

  final fallbackLocaleName = lookup.fallbackLocale;
  if (fallbackLocaleName != null && fallbackLocaleName != localeName) {
    // load messages for fallback locale, so it can be used if some key was not found
    await initializeMessages(fallbackLocaleName);
  }

  return res;
}