Unlock the Power of Mobile App Localization
In today’s digital landscape, reaching a global audience is crucial for mobile app success. With over 90% of mobile app downloads coming from non-English speaking countries, adapting your app to cater to diverse locales is no longer a luxury, but a necessity.
Easy Localization: A Game-Changer for Flutter Apps
Enter easy_localization, a powerful package designed specifically for Flutter mobile apps. This innovative solution enables you to effortlessly engineer your app to reach a broader audience, supporting language translations, gender, text direction, pluralization, and more.
Getting Started with Easy Localization
To harness the full potential of easy_localization, follow these simple steps:
- Install and Set Up:
dependencies: easy_localization: ^2.3.0
Run
flutter pub get
to install the package. - Configure Your App:
-
- Create an
assets
folder. - Add translation files to the
assets
folder. - Declare the
assets
folder in yourpubspec.yaml
file:
- Create an
flutter: assets: - assets/
-
- Generate Locale Keys:
Run the command to generate a
locale_keys.g.dart
file, containing JSON keys from your translation files:flutter pub run easy_localization:generate
App Configuration and Localization
Next, configure your app to support localization by:
- Creating a Supported Locales Class:
class SupportedLocales { static const List<Locale> locales = [ Locale('en', 'US'), Locale('fr', 'FR'), Locale('es', 'ES'), ]; }
- Updating Your Main Function:
void main() { WidgetsFlutterBinding.ensureInitialized(); EasyLocalization.ensureInitialized().then((_) { runApp(MyApp()); }); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return EasyLocalization( supportedLocales: SupportedLocales.locales, path: 'assets/translations', fallbackLocale: Locale('en', 'US'), child: MaterialApp( title: 'Easy Localization Demo', home: MyHomePage(), ), ); } }
Usage and Interpolation
Now, it’s time to put easy_localization into action:
Access Localized Values:
Text(LocaleKeys.hello.tr())
Display Localized Content:
Text(LocaleKeys.author.tr(args: ['John Doe']))
Want to retain certain words in their original language? Interpolation is the answer. Pass in arguments in the translate
function, and use placeholders in your JSON translation files:
{
"hello": "Hello, {name}!",
"author": "Author: {author}"
}
Linked Translation and Directionality
Take your localization to the next level with:
- Linked Translation:
Link to translated text using the
@:key
syntax, reducing costs and effort:Text(LocaleKeys.about.tr(@:about_us))
- Directionality:
Use locale-direction aware widgets to ensure correct layout updates based on the locale:
Directionality( textDirection: context.locale.textDirection, child: Text(LocaleKeys.welcome.tr()), )
The Future of Mobile App Localization
With easy_localization, you’re just a few steps away from unlocking the full potential of your mobile app. By following this tutorial, you’ll be able to:
- Add and load translation files
- Use code generation for localization keys
- React and persist to system locale changes
- Use interpolation in translated texts
- Link translations
- Display text correctly based on locale directionality
Get started today and watch your app thrive in a global market!