lookupByLanguageCode static method

Country? lookupByLanguageCode(
  1. String languageCode
)

Resolve country by language code.

Implementation

static Country? lookupByLanguageCode(String languageCode) {
  final normalized = languageCode.trim().replaceAll('_', '-');
  if (normalized.isEmpty) {
    return null;
  }

  final segments = normalized.split('-');
  if (segments.length > 1) {
    final regionCode = segments.last.toUpperCase();
    final regionCountry = CountryData.getCountryByCode(regionCode);
    if (regionCountry != null) {
      return regionCountry;
    }
  }

  final language = segments.first.toLowerCase();
  final mappedCountryCode = _languageToCountryCode[language];
  if (mappedCountryCode == null) {
    return null;
  }

  return CountryData.getCountryByCode(mappedCountryCode);
}