ez_localization
Setup a powerful localization system in your flutter application quickly and easily.
Localize your flutter application quickly and easily.
English project snapshot. Visit GitHub for the latest content.
This package allows you to setup a powerful localization system with ease and in only a few minutes.
Here are some features:
It only takes a few steps in order to get EzLocalization to work !
First, add the following code to your MaterialApp definition (usually in main.dart) :
EzLocalizationDelegate ezLocalization = EzLocalizationDelegate(supportedLocales: [Locale('en'), Locale('fr')]); // The first language is your default language.
return MaterialApp(
// ...
localizationsDelegates: ezLocalization.localizationDelegates,
supportedLocales: ezLocalization.supportedLocales,
localeResolutionCallback: ezLocalization.localeResolutionCallback,
);
Then you create a folder named languages in your assets directory with the defined languages in it.
An example structure could be :
assets
└── languages
├── en.json
└── fr.json
Here's an example of en.json :
{
"hello": "Hello !"
}
And a translated fr.json :
{
"hello": "Bonjour !"
}
Don't forget to add the assets in your pubspec.yml :
flutter:
# ...
assets:
- "assets/languages/"
That's it ! To get your string you only have to call EzLocalization.of(context)!.get('hello').
With the extension method, it's even easier to get a localized string !
The only thing you have to do is to replace EzLocalization.of(context)!.get('key') by context.getString('key').
You may have to manually import EzLocalization in your file.
EzLocalization provides a builder widget called EzLocalizationBuilder. You can use it as such :
const EzLocalizationBuilder(
delegate: EzLocalizationDelegate(
supportedLocales: [
Locale('en'),
Locale('fr'),
Locale('es'),
],
),
builder: (context, localizationDelegate) => MaterialApp(
title: 'Beautifully localized app',
home: MyMainWidget(),
localizationsDelegates: localizationDelegate.localizationDelegates,
supportedLocales: localizationDelegate.supportedLocales,
localeResolutionCallback: localizationDelegate.localeResolutionCallback,
),
);
It has two advantages :
EzLocalizationBuilder.of(context)!.changeLocale(yourLocale).You can nest translation strings as such :
{
"tabs": {
"home": "Home"
}
}
And it can be access using EzLocalization.of(context)!.get('tabs.home').
In your translation string, you may add arguments using {} :
{
"greeting": "Hello {target}, my name is {me} !"
}
You can then fill them with EzLocalization.of(context)!.get('greeting', {'target': 'John', 'me': 'Bob'}).
Also, instead of a map you can pass a list and get your arguments by their indexes.
You can change from the default path of assets/languages/$languageCode.json by passing getPathFunction
to EzLocalizationDelegate. You will then have to provide a valid asset path according to the specified locale.
Don't forget to update your assets entry in your pubspec !
See the official flutter.dev documentation about updating the iOS app bundle.
You have a lot of options to contribute to this project ! You can :
Dependency list 3 items
{"sdk":"flutter"}{"sdk":"flutter"}^3.0.1Start with your use case, compare repository activity, and check the original project before adopting it.
“A repository is the most basic element of GitHub.”
GitHub Docs — About repositories ↗