language_code 0.7.1
language_code: ^0.7.1 copied to clipboard
Get the current language code and Locale of the device. Also includes almost the language codes with English names and native names.
Language Code #
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
LocaleandLanguageCodes - 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
StateErrorunless anorElseis provided.
Optional default fallback #
final code = LanguageCode.tryCode(defaultCode: LanguageCodes.en);
Fallback order:
- Full
rawLocale rawLocale.languageCodedefaultCode(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:
- Exact match (language + script + country)
- Script match (language + script)
- Country match (language + country)
- 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.