language_info_plus
is a Flutter package designed to provide a comprehensive list of world languages with localized names and ISO 639-1 codes. It helps developers display language information customized to the user's device locale.
- Extensive Language Data: Access a wide range of languages with their ISO codes and English names.
- Localization: Optionally load localized language names based on the device's locale.
- Easy Integration: Simplified APIs to access all languages, the device language, or retrieve a language by its code.
- Optional Initialization: Call
initializeLanguage()
in your app'smain()
to load localized language names.
If you want to use the localizedName
field for languages, initialize it first by loading the appropriate JSON file based on the device's locale.
import 'package:language_info_plus/language_info_plus.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// IMPORTANT: Call this to load localized language names, if needed.
await initializeLanguage();
runApp(const MyApp());
}
// Getting all available languages
List<Language> languages = LanguageInfoPlus.languages;
print('Total languages: ${languages.length}');
// Getting the device locale
Locale deviceLocale = LanguageInfoPlus.deviceLocale;
print('Device Locale: ${deviceLocale.toString()}');
// Getting the device language (based on locale)
Language deviceLanguage = LanguageInfoPlus.deviceLanguage;
print('Device Language: ${deviceLanguage.name}');
// Fetching a specific language by ISO 639-1 code
Language? languageEN = LanguageInfoPlus.getLanguageByCode('en');
print('Language Name: ${languageEN?.name}');
print('Localized Name: ${languageEN?.localizedName ?? "N/A"}');
The language_info_plus
library provides a set of static methods and properties through the LanguageInfoPlus
class:
API | Return Type | Description |
---|---|---|
LanguageInfoPlus.languages |
List<Language> |
Returns a list of all available languages. |
LanguageInfoPlus.deviceLocale |
Locale |
The current device locale derived from WidgetsBinding.instance.platformDispatcher.locale . |
LanguageInfoPlus.deviceLanguage |
Language |
Matches the device’s language code to a Language . |
LanguageInfoPlus.getLanguageByCode(String languageCode) |
Language? |
Fetches a Language by its ISO 639-1 code (e.g., “en”, “ko”). Returns null if not found. |
initializeLanguage() |
Future<void> |
Loads localized language names. Must be called before accessing localizedName . Recommended in main(). |
Each language is represented by the Language
class with the following properties:
Property | Type | Description |
---|---|---|
code |
String |
ISO 639-1 code (e.g., en , ko ). |
name |
String |
English name of the language (e.g., English , Korean ). |
localizedName |
String? |
Localized name of the language (e.g., 한국어 ). Nullable if not initialized. |
Example usage of Language
:
// Assuming the device locale's language is zh.
final language = LanguageInfoPlus.getLanguageByCode('en');
if (language != null) {
print('Code: ${language.code}'); // en
print('Name: ${language.name}'); // English
print('Localized Name: ${language.localizedName ?? "No localized name loaded"}'); // 英语
}
Contributions are welcome! Please open issues and pull requests to improve this package.