Language Code

codecov

A Dart package for working with language codes and device locales.
Includes a complete set of ISO 639-1 (2-letter) and ISO 639-2 (3-letter) codes with English and native names.


Features

  • Detect the device’s current language and locale
  • Convert between Locale and LanguageCodes
  • Look up by code, English name, or native name
  • Includes almost all ISO 639 codes with English + native names
  • Test-friendly (override locale or code in tests)
  • Auto-generated from Wikipedia’s ISO 639-2 list

Usage

Get the current device language

final languageCode = LanguageCode.code; // as LanguageCodes
final locale = LanguageCode.locale;     // as Locale

Get the raw device locale

May not be supported by LanguageCodes:

final rawLocale = LanguageCode.rawLocale;

Get language details

  • code: language code.
  • englishName: preferred English name.
  • nativeName: preferred native name.
  • nativeLatinName: preferred native latin name (if available; empty string if unavailable).
  • nativeNonLatinName: preferred native non-latin name (if available; empty string if unavailable).
var language = LanguageCodes.en;
print(language.englishName); // 'English'
print(language.nativeName);  // 'English'

language = LanguageCodes.vi;
print(language.englishName); // 'Vietnamese'
print(language.nativeName);  // 'Tiếng Việt'
language = LanguageCodes.ar;
print(language.code); // 'ar'
print(language.englishName); // 'Arabic'
print(language.nativeName); // 'العربية'
print(language.nativeLatinName); // "al'Arabiyyeẗ"
print(language.nativeNonLatinName); // 'العربية'

Store-supported language codes

The package also includes the language codes supported by each app store:

StoreSupportedCodes.playStore;
StoreSupportedCodes.appStore;

Use bothStores when you need to map a base language to the exact store-specific codes:

final variants = StoreSupportedCodes.bothStores[LanguageCodes.en];

playStore and appStore are the raw supported code sets, while bothStores groups the matching Play Store and App Store values by normalized two-letter language code.

For Hebrew, the legacy Play Store locale iw_IL is now represented as he_IL.

Convert values to LanguageCodes

LanguageCodes.fromLocale(Locale('vi'));        // → LanguageCodes.vi
LanguageCodes.fromCode('vi');                  // → LanguageCodes.vi
LanguageCodes.fromEnglishName('Vietnamese');   // → Iterable<LanguageCodes>
LanguageCodes.fromNativeName('Tiếng Việt');    // → Iterable<LanguageCodes>

⚠️ If no matching element is found, the methods throw a StateError unless an orElse is provided.

Optional default fallback

final code = LanguageCode.tryCode(defaultCode: LanguageCodes.en);

Fallback order:

  1. Full rawLocale
  2. rawLocale.languageCode
  3. defaultCode (default: LanguageCodes.und)

Testing

Override values in unit tests:

LanguageCode.setTestCode(LanguageCodes.vi);
// or
LanguageCode.setTestLocale(const Locale('fr'));

Reset with null to restore normal behavior.

⚠️ You can only use one override at a time.

Locale matching

Use LanguageCodes.resolveFromLocale or LanguageCodes.tryResolveFromLocale when you want robust matching across variants. Matching is attempted in this order:

  1. Exact match (language + script + country)
  2. Script match (language + script)
  3. Country match (language + country)
  4. Language-only

Example:

final code = LanguageCodes.resolveFromLocale(
	Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'),
);
// → LanguageCodes.zh_Hans_CN

Contributions

Missing or incorrect language? Please open an issue or create a PR. The codes are auto-generated via a crawler, but feedback is always welcome.

Libraries

language_code