localeResolutionCallback method

Locale? localeResolutionCallback(
  1. Locale? preferredLocale,
  2. Iterable<Locale>? supportedLocales
)

Called by the LocalizationsDelegate. Possibly needed to resolve current Locale.

Implementation

Locale? localeResolutionCallback(
    Locale? preferredLocale, Iterable<Locale>? supportedLocales) {
  //
  // In case this function was not called yet.
  _initLocales(textLocale, l10nMap);

  if (preferredLocale == null) {
    // Retrieve the 'first' locale in the supported locales.
    final locales = _supportedLocales;

    if (locales.isNotEmpty) {
      //
      if (supportedLocales == null) {
        // Use the first supported locale.
        preferredLocale = locales.first;
      } else {
        // Find the first supported locale
        for (final _locale in supportedLocales.toList(growable: false)) {
          //
          if (locales.contains(_locale)) {
            preferredLocale = _locale;
          }
        }
      }
    }
  }

  // The full system-reported supported locales of the device.
  final systemLocales = WidgetsBinding.instance.window.locales;

  if (_appLocale == null) {
    _appLocale = preferredLocale;
  } else {
    // If the passed Locale is unique but supported.
    if (systemLocales.isNotEmpty) {
      if (!systemLocales.contains(preferredLocale) &&
          supportedLocales != null &&
          supportedLocales.contains(preferredLocale)) {
        // It's a specific Locale assigned to the app
        _appLocale = preferredLocale;
      } else {
        // Override with the app's preferred locale.
        preferredLocale = _appLocale;
      }
    }
  }

  // Determine the device's default Locale. Assume the first trip will do it.
  _deviceLocale ??= systemLocales.isNotEmpty ? systemLocales.first : null;

  if (_backupLocale == null &&
      supportedLocales != null &&
      supportedLocales.length > 1) {
    final list = supportedLocales.toList(growable: false);
    // Assume the second Locale a suitable backup.
    _backupLocale = list[1];
  }
  return preferredLocale;
}