Skip to content

Commit

Permalink
add _supportedLocales in EasyLocalizationController; check the device… (
Browse files Browse the repository at this point in the history
#672)

* add _supportedLocales in EasyLocalizationController; check the deviceLocale in resetLocale; add scriptCode if useOnlyLangCode

* add changelog

* add scriptCode when assigning the desiredLocale

* in resetLocale(), log the _deviceLocale and _fallbackLocale and the selected locale

---------

Co-authored-by: l <[email protected]>
  • Loading branch information
bbsmartc and l authored May 3, 2024
1 parent 39d091e commit 0150d7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
### [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;
- add scriptCode to desiredLocale if useOnlyLangCode is true. scriptCode is needed sometimes, for example zh-Hans, zh-Hant

### [3.0.5]

Expand Down
10 changes: 7 additions & 3 deletions lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EasyLocalizationController extends ChangeNotifier {

late Locale _locale;
Locale? _fallbackLocale;
List<Locale>? _supportedLocales;

final Function(FlutterError e) onLoadError;
final AssetLoader assetLoader;
Expand All @@ -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) {
Expand Down Expand Up @@ -144,8 +146,9 @@ class EasyLocalizationController extends ChangeNotifier {
final result = <String, dynamic>{};
final loaderFutures = <Future<Map<String, dynamic>?>>[];

// 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<AssetLoader> loaders = [
assetLoader,
Expand Down Expand Up @@ -203,9 +206,10 @@ class EasyLocalizationController extends ChangeNotifier {
Locale get deviceLocale => _deviceLocale;

Future<void> resetLocale() async {
EasyLocalization.logger('Reset locale to platform locale $_deviceLocale');
final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale);

await setLocale(_deviceLocale);
EasyLocalization.logger('Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale');
await setLocale(locale);
}
}

Expand Down

0 comments on commit 0150d7e

Please sign in to comment.