Skyost / EzLocalization

Localize your flutter application quickly and easily.

Home Page:https://pub.dev/packages/ez_localization

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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);
}

Hi @Skyost, thanks for your comment. Yes, I used a similar way to implement my feature. I just thought it would be a nice thing if this is done in the package.
But, if you feel like this is something the plugin should not do. You can close this issue.
Cheers!