diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b4f9c00..75892cb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - add _supportedLocales in EasyLocalizationController; log and check the deviceLocale when resetLocale; - add scriptCode to desiredLocale if useOnlyLangCode is true. scriptCode is needed sometimes, for example zh-Hans, zh-Hant - add savedLocale get method for context. if context.savedLocale is null, then language option is `following system`, i can display the option in user selection form. +- fix the bug: _savedLocale not assigned in _saveLocale(), and notify listeners after _saveLocale() because _savedLocale might be needed +- saveLocale even if the locale not changed, when using _savedLocale as `following system` option and currently system language is en, then change the locale from system to en, won't really change the locale but needs to store it to distinguish the difference of `system` and `english` +- `shouldReload(LocalizationsDelegate old)` return true, the LocalizationsDelegate does not reload when return false, tested in my current configuration. ### [3.0.5] diff --git a/lib/src/easy_localization_app.dart b/lib/src/easy_localization_app.dart index 21629f49..0b97e493 100644 --- a/lib/src/easy_localization_app.dart +++ b/lib/src/easy_localization_app.dart @@ -252,6 +252,15 @@ class _EasyLocalizationProvider extends InheritedWidget { assert(parent.supportedLocales.contains(locale)); await _localeState.setLocale(locale); } + else { + // given current language en (option is following system, en is the system language) + // then set the locale to en won't change the locale + // but needs change the savedLocale + // savedLocale == null will give a language following the system + // savedLocale == Locale('en') will give english + assert(parent.supportedLocales.contains(locale)); + await _localeState.storeLocale(locale); + } } /// Clears a saved locale from device storage @@ -312,5 +321,5 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate { } @override - bool shouldReload(LocalizationsDelegate old) => false; + bool shouldReload(LocalizationsDelegate old) => true; } diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 494c344b..3740da00 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -174,14 +174,20 @@ class EasyLocalizationController extends ChangeNotifier { Future setLocale(Locale l) async { _locale = l; + EasyLocalization.logger('Locale $locale changed'); + await _saveLocale(_locale); await loadTranslations(); notifyListeners(); - EasyLocalization.logger('Locale $locale changed'); + } + + // avoid name conflict with the variable saveLocale + Future storeLocale(Locale? locale) async { await _saveLocale(_locale); } Future _saveLocale(Locale? locale) async { if (!saveLocale) return; + _savedLocale = locale; final preferences = await SharedPreferences.getInstance(); await preferences.setString('locale', locale.toString()); EasyLocalization.logger('Locale $locale saved');