A way to preserve the changed locale.
srihariash999 opened this issue · comments
Is your feature request related to a problem? Please describe.
The locale is changed using builder and it changes instantly which I am thankful for. But if I restart the app, it defaults to the original language.
Describe the solution you'd like
It'd be great if the last locale choice is preserved and it is loaded on next start. Maybe using shared preferences?
Describe alternatives you've considered
Currently, only way I can think of is by forcing a locale after getting it from stored key using shared preferences, but I'm not very comfortable forcing a locale.
Additional context
Is there a way to supply a locale as a first preference?
Well, this is not the purpose of this plugin ; EzLocalization only provide you a way to localize your app easily, it doesn't have to know/manage user preferences. You could use a plugin like shared_preferences
to save the chosen locale and load it at startup 😉
Is there a way to supply a locale as a first preference?
Sure, just use shared_preferences
, and if the current locale is not present, use your default locale.
Here's an example to load a saved locale :
Future<Locale> loadDefaultLocale() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
String currentLocale = preferences.getString('current_locale') ?? 'en';
return Locale(currentLocale);
}
And if you want to save it, just use something like :
Future<void> saveDefaultLocale(Locale currentLocale) async {
SharedPreferences preferences = await SharedPreferences.getInstance();
preferences.setString('current_locale', currentLocale.languageCode);
}