| name | localization |
| description | Explains and changes GetX localization in this app: default language, switching language, adding a language, and adding string keys. Use when the user mentions localization, translation, locale, language, Strings.tr, i18n, or LocalizationService. |
Localization
GetX Translations with keys in Strings, and one map file per language. Current locale is stored in shared preferences.
How it works
GetMaterialApp uses:
locale: MySharedPref.getCurrentLocal(),
translations: LocalizationService.getInstance(),
Service shape:
class LocalizationService extends Translations {
static Locale defaultLanguage = supportedLanguages['en']!;
static Map<String, Locale> supportedLanguages = {
'en': const Locale('en', 'US'),
'ar': const Locale('ar', 'AR'),
};
static Map<String, TextStyle> supportedLanguagesFontsFamilies = {
'en': const TextStyle(fontFamily: 'Poppins'),
'ar': const TextStyle(fontFamily: 'Cairo'),
};
@override
Map<String, Map<String, String>> get keys => {
'en_US': enUs,
'ar_AR': arAR,
};
}
Keys:
class Strings {
static const String hello = 'hello';
}
Per-language maps (every Strings.* key must appear in every map):
const Map<String, String> enUs = {
Strings.hello: 'Hello!',
};
final Map<String, String> arAR = {
Strings.hello: 'مرحباً!',
};
In widgets:
Text(Strings.hello.tr)
Switch language:
LocalizationService.updateLanguage('en'); // or 'ar'
updateLanguage checks supportedLanguages, writes shared pref, then Get.updateLocale.
Read locale:
LocalizationService.getCurrentLocal();
// or
MySharedPref.getCurrentLocal();
If shared pref has no value, it returns LocalizationService.defaultLanguage.
Change default language
Set:
static Locale defaultLanguage = supportedLanguages['en']!;
to the supported code the user wants ('ar', 'fr', …). That code must already exist in supportedLanguages. If it does not, add the language first.
Done when defaultLanguage points at the requested supported locale.
Change language (runtime)
Call LocalizationService.updateLanguage('{code}') with a key from supportedLanguages (en, ar, …). If the code is not supported, add the language first, then switch.
Add a string key
- Add
static const String {camel} = '{snake or short key}'; on Strings.
- Add
{Strings.{camel}: '...'} to every existing language map with the real translation (ask the user for copy you do not know).
- Use
Strings.{camel}.tr in UI.
Done when the key exists on Strings and in every language map.
Add a language
Need a language code (fr) and a locale (fr, FR).
supportedLanguages['fr'] = const Locale('fr', 'FR');
- Font family for that code in
supportedLanguagesFontsFamilies. The family must already be registered in assets/fonts/ and pubspec.yaml. If it is not, stop and say the font files are missing. Until then you may temporarily reuse Poppins and tell the user.
- New file
lib/config/translations/fr_FR/fr_fr_translation.dart:
import '../strings_enum.dart';
const Map<String, String> frFR = {
Strings.hello: 'Bonjour !',
// every existing Strings.* key must have a value here
};
Copy the full key set from an existing map, then fill translations. If the user did not provide them, keep English as a placeholder and tell them which keys still need copy.
- Import that map and add
'fr_FR': frFR to keys.
The keys map key is {language}_{COUNTRY} matching the Locale (en_US, ar_AR, fr_FR).
Done when supportedLanguages, keys, the new map file, and supportedLanguagesFontsFamilies all include the language, and the new map contains every Strings key.