From 53b4bef350c51710d12897b85f27e815a31aca88 Mon Sep 17 00:00:00 2001 From: l Date: Wed, 1 May 2024 13:53:05 +0800 Subject: [PATCH 1/4] add _supportedLocales in EasyLocalizationController; check the deviceLocale in resetLocale; add scriptCode if useOnlyLangCode --- lib/src/easy_localization_controller.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index eda3cbe4..96a7999c 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -12,6 +12,7 @@ class EasyLocalizationController extends ChangeNotifier { late Locale _locale; Locale? _fallbackLocale; + List? _supportedLocales; final Function(FlutterError e) onLoadError; final AssetLoader assetLoader; @@ -38,6 +39,7 @@ class EasyLocalizationController extends ChangeNotifier { Locale? forceLocale, // used for testing }) { _fallbackLocale = fallbackLocale; + _supportedLocales = supportedLocales; if (forceLocale != null) { _locale = forceLocale; } else if (_savedLocale == null && startLocale != null) { @@ -144,8 +146,9 @@ class EasyLocalizationController extends ChangeNotifier { final result = {}; final loaderFutures = ?>>[]; + // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode final Locale desiredLocale = - useOnlyLangCode ? Locale(locale.languageCode) : locale; + useOnlyLangCode ? Locale.fromSubtags(languageCode: locale.languageCode, scriptCode: locale.scriptCode) : locale; List loaders = [ assetLoader, @@ -205,7 +208,8 @@ class EasyLocalizationController extends ChangeNotifier { Future resetLocale() async { EasyLocalization.logger('Reset locale to platform locale $_deviceLocale'); - await setLocale(_deviceLocale); + final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale); + await setLocale(locale); } } From fa7415d2f9bdb7c2ad236143c368ece587801e93 Mon Sep 17 00:00:00 2001 From: l Date: Fri, 3 May 2024 08:49:55 +0800 Subject: [PATCH 2/4] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f4e7c1e..b209588e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### [3.0.6] - add 'useFallbackTranslationsForEmptyResources' to be able to use fallback locales for empty resources. +- add _supportedLocales in EasyLocalizationController; log and check the deviceLocale when resetLocale; ### [3.0.5] From 2bd31dacb8361a290738e59f349f20db69880944 Mon Sep 17 00:00:00 2001 From: l Date: Fri, 3 May 2024 08:53:45 +0800 Subject: [PATCH 3/4] add scriptCode when assigning the desiredLocale --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b209588e..267f04ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,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 ### [3.0.5] From 1bbaac5b71ceb898397cc98a005953424ae0ff00 Mon Sep 17 00:00:00 2001 From: l Date: Fri, 3 May 2024 15:31:19 +0800 Subject: [PATCH 4/4] in resetLocale(), log the _deviceLocale and _fallbackLocale and the selected locale --- lib/src/easy_localization_controller.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 96a7999c..983fb265 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -206,9 +206,9 @@ class EasyLocalizationController extends ChangeNotifier { Locale get deviceLocale => _deviceLocale; Future resetLocale() async { - EasyLocalization.logger('Reset locale to platform locale $_deviceLocale'); - final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale); + + EasyLocalization.logger('Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale'); await setLocale(locale); } }