parseRawLocale method

E parseRawLocale(
  1. String rawLocale
)

Parses the raw locale to get the enum. Fallbacks to base locale.

Implementation

E parseRawLocale(String rawLocale) {
  final match = _localeRegex.firstMatch(rawLocale);
  AppLocaleId? selected;
  if (match != null) {
    final language = match.group(1);
    final country = match.group(5);

    // match exactly
    selected = localeIds.cast<AppLocaleId?>().firstWhere(
        (supported) =>
            supported?.languageTag == rawLocale.replaceAll('_', '-'),
        orElse: () => null);

    if (selected == null && language != null) {
      // match language
      selected = localeIds.cast<AppLocaleId?>().firstWhere(
          (supported) => supported?.languageTag.startsWith(language) == true,
          orElse: () => null);
    }

    if (selected == null && country != null) {
      // match country
      selected = localeIds.cast<AppLocaleId?>().firstWhere(
          (supported) => supported?.languageTag.contains(country) == true,
          orElse: () => null);
    }
  }

  if (selected == null) {
    return baseLocale;
  }

  return mapper.toEnum(selected) ?? baseLocale;
}