parseAppLocale method

E parseAppLocale(
  1. BaseAppLocale<BaseTranslations> locale
)

Gets the E type of locale.

locale may have a locale which is unsupported by E. In this case, the base locale will be returned.

Implementation

E parseAppLocale(BaseAppLocale locale) {
  E? selected;

  // match exactly
  selected = locales.firstWhereOrNull((supported) => supported == locale);

  if (selected == null) {
    // match language
    selected = locales.firstWhereOrNull((supported) {
      return supported.languageCode == locale.languageCode;
    });
  }

  if (selected == null && locale.countryCode != null) {
    // match country
    selected = locales.firstWhereOrNull((supported) {
      return supported.countryCode == locale.countryCode;
    });
  }

  return selected ?? baseLocale;
}