How add a new language into Flutter app?
Estimated reading: 1 minutes
Step 1 - Open Your Project
Open your project in your preferred development IDE.
Step 2 - Generate Language Dart File
Navigate to the lib → locale folder.
Create a new Dart file for your new language. For example, if you want to add German, create a file with name
language_de.dart
.Inside
language_de.dart
, define a class name LanguageDe and translate all strings into German.tipYou can follow the convention of naming them as language_languageCode.dart, where languageCode represents the ISO 639-1 language code for the language.
For example:
English: language_en.dart Spanish: language_es.dart French: language_fr.dart
Step 3 - Update App Localizations
Open the
app_localizations.dart
file.Inside a
switch
statement, add the following code to handle the new language:case 'de':
return LanguageDe();
Step 4 - Add Flag Image
Go to the assets → flags folder.
Add a flag image for newly added language. For example, for French, name the image file as
ic_de.png
.Add constant for image file in "lib → utils → images.dart"
const ic_de = 'assets/flags/ic_de.png';
Step 5 - Update Common Base
Open the
lib → utils → common_base.dart
file.Locate the
languageList()
method.Add a new entry for the added language in the format:
LanguageDataModel(
id: 5,
name: 'German',
languageCode: 'de',
fullLanguageCode: 'de-DE',
flag: ic_de,
),
Ensure to use the correct language code in both languageCode
and fullLanguageCode
.
If you don't know what's language code for your preferred language please do visit this site
for getting language code - Click Me!