parse method

E parse(
  1. String rawLocale
)

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

Implementation

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

    // match exactly
    selected = locales.firstWhereOrNull((supported) =>
        supported.languageTag == rawLocale.replaceAll('_', '-'));

    if (selected == null && language != null) {
      // match language
      selected = locales.firstWhereOrNull(
          (supported) => supported.languageCode == language);
    }

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

  return selected ?? baseLocale;
}