This document explains how to handle internationalization and localization in the SparX Wallet Flutter project.
This project relies on flutter_localizations and follows the official internationalization guide for Flutter. However, we use easy_localization package to simplify the internationalization process.
To generate localization files for all packages, run:
melos run codegen:locale- To add a new localizable string, open the
en.jsonfile atassets/translations/en.json.
{
"confirm": "Confirm"
}- Then add a new key/value:
{
"confirm": "Confirm",
"continueWord": "Continue"
}- Use the new string in your Dart code:
import 'package:app/generated/generated.dart';
@override
Widget build(BuildContext context) {
return Text(LocaleKeys.continueWord.tr());
}Update the CFBundleLocalizations array in the Info.plist at ios/Runner/Info.plist to include the new locale:
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>es</string>
</array>Android configuration is automatically handled by the Flutter framework. No additional steps are required.
- For each supported locale, add a new JSON file in
assets/translations:
βββ assets
β βββ translations
β β βββ en.json
β β βββ es.json
- Add the translated strings to each JSON file:
For example, es.json:
{
"confirm": "Confirmar",
"continueWord": "Continuar"
}- Provide a flag icon asset for the language selector:
Add a new vector icon to assets/images/lang_icons/spanish.svg.
- Add the locale to
SupportedLocaleCodesenum inlib/app/service/localization/service/supported_locale_codes.dart:
enum SupportedLocaleCodes {
en(LocaleKeys.langEnglish),
es(LocaleKeys.langSpanish),
// other languages...
}- Provide a flag icon asset path in the
SupportedLocaleCodesextension:
extension SupportedLocaleCodesExt on SupportedLocaleCodes {
String get iconPath => switch (this) {
SupportedLocaleCodes.en => Assets.images.langIcons.english.path,
SupportedLocaleCodes.es => Assets.images.langIcons.spanish.path,
// other languages...
};
}When implementing new features, ensure all user-facing strings are properly localized. You can test different localizations by changing the language in the app settings or by using the appropriate Flutter testing tools.
- Don't Hardcode Strings: Always use the localization system for any user-facing text
- Use Descriptive Keys: Create logical, structured keys (e.g.,
auth.login.buttoninstead oflogin_btn) - Include Context Comments: When necessary, include context information for translators
- Test with Different Languages: Ensure UI adapts well to languages with different text lengths
- Handle Plurals Properly: Use the appropriate plural forms for different languages
- Consider Right-to-Left Languages: Ensure your UI works well with RTL languages if supported
- If localized strings are not showing up, ensure you have run
melos run codegen:locale - For missing keys, check that they exist in all language JSON files
- For UI layout issues with translations, consider using more flexible layouts to accommodate different text lengths