Flutter Localization: Best Practices and Advanced Techniques
The localization in Flutter acts as the master UX during the app, so all businesses with an unpractical audience’s language would like to implement 2 or more languages in the system.
After the implementation, add your keys and values plus use the localization class and pass the context to get the keys which is the normal case.?
Now we go to the services or helper class which is no way to pass the context or build it but you need to show the error as a TOAST maybe, we have many ways to do this task like the following:
?1- you can make lists one for each language which each has a static value in each language and toggle between the lists depending on the Local key or the language key whatever way which is you keep it to use ( btw, it is not that good).
2- you can use the context by the GLOBAL_CONTEXT, which great solution but you will face a problem which is that you have to restart the app every time you change the language which is not that efficient.
Maybe there’s another solution there but check this TIP:
You can use the localization as it bypasses the Local key to the “AppLocalization” Class and then use the keys from arb files check the next line :?
???
extension ManualLocalization on String {
// Manually load the localization
Future<AppLocalizations> get manualSetLocalization async => await AppLocalizations.delegate.load(Locale(this));
}
So you can pass the Local Key from wherever you save like that:
localKey.manualSetLocalization.APP_LOCALIZATION_CLASS_KEY;
And that’s it now you can use it everywhere without context.