From 066c7c4a1a6a78db7128bfbbc0f7e32ac918e7b9 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 4 May 2024 15:15:53 +0800 Subject: [PATCH 1/5] add savedLocale get method for context --- CHANGELOG.md | 1 + lib/src/easy_localization_app.dart | 1 + lib/src/easy_localization_controller.dart | 1 + lib/src/public_ext.dart | 1 + 4 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 267f04ae..8b4f9c00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - add 'useFallbackTranslationsForEmptyResources' to be able to use fallback locales for empty resources. - 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. ### [3.0.5] diff --git a/lib/src/easy_localization_app.dart b/lib/src/easy_localization_app.dart index 0a49e9a0..21629f49 100644 --- a/lib/src/easy_localization_app.dart +++ b/lib/src/easy_localization_app.dart @@ -261,6 +261,7 @@ class _EasyLocalizationProvider extends InheritedWidget { /// Getting device locale from platform Locale get deviceLocale => _localeState.deviceLocale; + Locale? get savedLocale => _localeState.savedLocale; /// Reset locale to platform locale Future resetLocale() => _localeState.resetLocale(); diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 983fb265..494c344b 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -204,6 +204,7 @@ class EasyLocalizationController extends ChangeNotifier { } Locale get deviceLocale => _deviceLocale; + Locale? get savedLocale => _savedLocale; Future resetLocale() async { final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale); diff --git a/lib/src/public_ext.dart b/lib/src/public_ext.dart index 876099a1..4b1aa878 100644 --- a/lib/src/public_ext.dart +++ b/lib/src/public_ext.dart @@ -165,6 +165,7 @@ extension BuildContextEasyLocalizationExtension on BuildContext { /// Getting device locale from platform Locale get deviceLocale => EasyLocalization.of(this)!.deviceLocale; + Locale? get savedLocale => EasyLocalization.of(this)!.savedLocale; /// Reset locale to platform locale Future resetLocale() => EasyLocalization.of(this)!.resetLocale(); From 7a9121d0b6afd3741c2efa95c9276ee939a6dc05 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 4 May 2024 16:41:06 +0800 Subject: [PATCH 2/5] assgin _savedLocale = locale when _saveLocale(); notify listeners after _saveLocale becaused it's needed by the ui --- lib/src/easy_localization_controller.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 494c344b..8c1b9146 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -174,14 +174,15 @@ class EasyLocalizationController extends ChangeNotifier { Future setLocale(Locale l) async { _locale = l; - await loadTranslations(); - notifyListeners(); EasyLocalization.logger('Locale $locale changed'); await _saveLocale(_locale); + await loadTranslations(); + notifyListeners(); } 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'); From cb1cecb102cd6bfe2748f8e1b3efe18cdf507808 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 4 May 2024 16:46:05 +0800 Subject: [PATCH 3/5] changes added to md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b4f9c00..a1d07d8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 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 ### [3.0.5] From 63168ee77fb21ee0b049d6a1419f876904dc36c5 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 4 May 2024 17:44:26 +0800 Subject: [PATCH 4/5] saveLocale even if the locale not changed --- lib/src/easy_localization_app.dart | 11 ++++++++++- lib/src/easy_localization_controller.dart | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 8c1b9146..3740da00 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -180,6 +180,11 @@ class EasyLocalizationController extends ChangeNotifier { notifyListeners(); } + // 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; From ad25fe2a70802fa9bb5798e2944009bad15b88e9 Mon Sep 17 00:00:00 2001 From: l Date: Sat, 4 May 2024 17:51:55 +0800 Subject: [PATCH 5/5] shouldReload(LocalizationsDelegate old) return true, and changelog added --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1d07d8e..75892cb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - 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]