tryFindLocale static method

LocaleName? tryFindLocale(
  1. String langCode, {
  2. IfLocaleNotFound ifLocaleNotFound = IfLocaleNotFound.doNothing,
})

Try to find Locale by string in LocaleSwitcher.supportedLocaleNames.

If not found, will do one of IfLocaleNotFound.

Implementation

// todo: similarity check?
static LocaleName? tryFindLocale(String langCode,
    {IfLocaleNotFound ifLocaleNotFound = IfLocaleNotFound.doNothing}) {
  var loc = byName(langCode) ?? byLanguage(langCode);
  if (loc != null) {
    return loc;
  }
  switch (ifLocaleNotFound) {
    case IfLocaleNotFound.doNothing:
      return null;
    case IfLocaleNotFound.useSystem:
      if (byName(systemLocale) != null) {
        return byName(systemLocale)!;
      }
      return null;
    case IfLocaleNotFound.useFirst:
      if (supported.names.first != systemLocale) {
        return supported.entries.first;
      } else if (supported.names.length > 2) {
        return supported.entries[1];
      }
      return null;
  }
}