From 149ef9140ba800796459f1e7a16ca56685b2aa8d Mon Sep 17 00:00:00 2001 From: Patrick Chrestin Date: Wed, 12 Jun 2024 23:55:39 +0200 Subject: [PATCH 1/4] fix strict countryCode problem --- lib/src/easy_localization_controller.dart | 33 ++++++++++--- test/easy_localization_test.dart | 58 ++++++++++++++++++----- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 494c344b..18d68a64 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -72,14 +72,29 @@ class EasyLocalizationController extends ChangeNotifier { }) { final selectedLocale = supportedLocales.firstWhere( (locale) => locale.supports(deviceLocale), - orElse: () => _getFallbackLocale(supportedLocales, fallbackLocale), + orElse: () => _getFallbackLocale( + supportedLocales, + fallbackLocale, + deviceLocale: deviceLocale, + ), ); return selectedLocale; } //Get fallback Locale static Locale _getFallbackLocale( - List supportedLocales, Locale? fallbackLocale) { + List supportedLocales, Locale? fallbackLocale, + {final Locale? deviceLocale}) { + if (deviceLocale != null) { + // a locale that matches the language code of the device locale is + // preferred over the fallback locale + final deviceLanguage = deviceLocale.languageCode; + for (Locale locale in supportedLocales) { + if (locale.languageCode == deviceLanguage) { + return locale; + } + } + } //If fallbackLocale not set then return first from supportedLocales if (fallbackLocale != null) { return fallbackLocale; @@ -146,9 +161,11 @@ 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.fromSubtags(languageCode: locale.languageCode, scriptCode: locale.scriptCode) : locale; + // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode + final Locale desiredLocale = useOnlyLangCode + ? Locale.fromSubtags( + languageCode: locale.languageCode, scriptCode: locale.scriptCode) + : locale; List loaders = [ assetLoader, @@ -207,9 +224,11 @@ class EasyLocalizationController extends ChangeNotifier { Locale? get savedLocale => _savedLocale; Future resetLocale() async { - final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, fallbackLocale: _fallbackLocale); + 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'); + EasyLocalization.logger( + 'Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale'); await setLocale(locale); } } diff --git a/test/easy_localization_test.dart b/test/easy_localization_test.dart index 59e21213..e4689a8f 100644 --- a/test/easy_localization_test.dart +++ b/test/easy_localization_test.dart @@ -225,6 +225,38 @@ void main() { zhHans, ); }); + + // New + test('select best lenguage match if no perfect match exists', () { // #674 + const userDeviceLocale = Locale('en', 'FR'); + const supportedLocale1 = Locale('en', 'US'); + const supportedLocale2 = Locale('zh', 'CN'); + + expect( + EasyLocalizationController.selectLocaleFrom( + [supportedLocale1, supportedLocale2], + userDeviceLocale, + fallbackLocale: supportedLocale2, + ), + supportedLocale1, + ); + }); + + test('select perfect match if exists', () { // #674 + const userDeviceLocale = Locale('en', 'GB'); + const supportedLocale1 = Locale('en', 'US'); + const supportedLocale2 = userDeviceLocale; + + expect( + EasyLocalizationController.selectLocaleFrom( + [supportedLocale1, supportedLocale2], + userDeviceLocale, + fallbackLocale: supportedLocale2, + ), + supportedLocale2, + ); + }); + // end new }); group('tr', () { @@ -526,13 +558,13 @@ void main() { test('two as fallback and fallback translations priority', overridePrint(() { - printLog = []; - expect( - Localization.instance.plural('test_empty_fallback_plurals', 2), - '', - ); - expect(printLog, isEmpty); - })); + printLog = []; + expect( + Localization.instance.plural('test_empty_fallback_plurals', 2), + '', + ); + expect(printLog, isEmpty); + })); test('with number format', () { expect( @@ -613,7 +645,8 @@ void main() { ); }); - test('two as fallback for empty resource and fallback translations priority', + test( + 'two as fallback for empty resource and fallback translations priority', overridePrint(() { printLog = []; expect( @@ -623,8 +656,7 @@ void main() { expect(printLog, isEmpty); })); - test('reports empty plural resource with fallback', - overridePrint(() { + test('reports empty plural resource with fallback', overridePrint(() { printLog = []; expect( Localization.instance.plural('test_empty_fallback_plurals', -1), @@ -647,8 +679,10 @@ void main() { expect(logIterator.current, contains('Localization key [test_empty_plurals.other] not found')); logIterator.moveNext(); - expect(logIterator.current, - contains('Fallback localization key [test_empty_plurals.other] not found')); + expect( + logIterator.current, + contains( + 'Fallback localization key [test_empty_plurals.other] not found')); })); }); From 5160e05978cf39e054499e8a2503e1e99023217c Mon Sep 17 00:00:00 2001 From: Patrick Chrestin Date: Thu, 13 Jun 2024 20:16:42 +0200 Subject: [PATCH 2/4] restore formatting --- lib/src/easy_localization_controller.dart | 12 ++++------- test/easy_localization_test.dart | 26 +++++++++++------------ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 18d68a64..0186843a 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -162,10 +162,8 @@ class EasyLocalizationController extends ChangeNotifier { final loaderFutures = ?>>[]; // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode - final Locale desiredLocale = useOnlyLangCode - ? Locale.fromSubtags( - languageCode: locale.languageCode, scriptCode: locale.scriptCode) - : locale; + final Locale desiredLocale = + useOnlyLangCode ? Locale.fromSubtags(languageCode: locale.languageCode, scriptCode: locale.scriptCode) : locale; List loaders = [ assetLoader, @@ -224,11 +222,9 @@ class EasyLocalizationController extends ChangeNotifier { Locale? get savedLocale => _savedLocale; Future resetLocale() async { - final locale = selectLocaleFrom(_supportedLocales!, deviceLocale, - fallbackLocale: _fallbackLocale); + 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'); + EasyLocalization.logger('Reset locale to $locale while the platform locale is $_deviceLocale and the fallback locale is $_fallbackLocale'); await setLocale(locale); } } diff --git a/test/easy_localization_test.dart b/test/easy_localization_test.dart index e4689a8f..e6640705 100644 --- a/test/easy_localization_test.dart +++ b/test/easy_localization_test.dart @@ -558,13 +558,13 @@ void main() { test('two as fallback and fallback translations priority', overridePrint(() { - printLog = []; - expect( - Localization.instance.plural('test_empty_fallback_plurals', 2), - '', - ); - expect(printLog, isEmpty); - })); + printLog = []; + expect( + Localization.instance.plural('test_empty_fallback_plurals', 2), + '', + ); + expect(printLog, isEmpty); + })); test('with number format', () { expect( @@ -645,8 +645,7 @@ void main() { ); }); - test( - 'two as fallback for empty resource and fallback translations priority', + test('two as fallback for empty resource and fallback translations priority', overridePrint(() { printLog = []; expect( @@ -656,7 +655,8 @@ void main() { expect(printLog, isEmpty); })); - test('reports empty plural resource with fallback', overridePrint(() { + test('reports empty plural resource with fallback', + overridePrint(() { printLog = []; expect( Localization.instance.plural('test_empty_fallback_plurals', -1), @@ -679,10 +679,8 @@ void main() { expect(logIterator.current, contains('Localization key [test_empty_plurals.other] not found')); logIterator.moveNext(); - expect( - logIterator.current, - contains( - 'Fallback localization key [test_empty_plurals.other] not found')); + expect(logIterator.current, + contains('Fallback localization key [test_empty_plurals.other] not found')); })); }); From aa32bd055f56d63ee338ef437fe118945a5fe20e Mon Sep 17 00:00:00 2001 From: Patrick Chrestin Date: Thu, 13 Jun 2024 20:18:03 +0200 Subject: [PATCH 3/4] restore formatting --- lib/src/easy_localization_controller.dart | 2 +- test/easy_localization_test.dart | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/src/easy_localization_controller.dart b/lib/src/easy_localization_controller.dart index 0186843a..2e62e2f8 100644 --- a/lib/src/easy_localization_controller.dart +++ b/lib/src/easy_localization_controller.dart @@ -161,7 +161,7 @@ class EasyLocalizationController extends ChangeNotifier { final result = {}; final loaderFutures = ?>>[]; - // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode + // need scriptCode, it might be better to use ignoreCountryCode as the variable name of useOnlyLangCode final Locale desiredLocale = useOnlyLangCode ? Locale.fromSubtags(languageCode: locale.languageCode, scriptCode: locale.scriptCode) : locale; diff --git a/test/easy_localization_test.dart b/test/easy_localization_test.dart index e6640705..9078d9ef 100644 --- a/test/easy_localization_test.dart +++ b/test/easy_localization_test.dart @@ -226,7 +226,6 @@ void main() { ); }); - // New test('select best lenguage match if no perfect match exists', () { // #674 const userDeviceLocale = Locale('en', 'FR'); const supportedLocale1 = Locale('en', 'US'); @@ -256,7 +255,6 @@ void main() { supportedLocale2, ); }); - // end new }); group('tr', () { @@ -558,7 +556,7 @@ void main() { test('two as fallback and fallback translations priority', overridePrint(() { - printLog = []; + printLog = []; expect( Localization.instance.plural('test_empty_fallback_plurals', 2), '', From c521c13a0207536e1d6e0f648961c814e0efde44 Mon Sep 17 00:00:00 2001 From: Patrick Chrestin Date: Fri, 14 Jun 2024 19:07:07 +0200 Subject: [PATCH 4/4] prefixed country code variable name with an underscore --- bin/generate.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/generate.dart b/bin/generate.dart index cb006022..6c503f2f 100644 --- a/bin/generate.dart +++ b/bin/generate.dart @@ -255,13 +255,13 @@ class CodegenLoader extends AssetLoader{ for (var file in files) { final localeName = path.basename(file.path).replaceFirst('.json', '').replaceAll('-', '_'); - listLocales.add('"$localeName": $localeName'); + listLocales.add('"$localeName": _$localeName'); final fileData = File(file.path); Map? data = json.decode(await fileData.readAsString()); final mapString = const JsonEncoder.withIndent(' ').convert(data); - gFile += 'static const Map $localeName = $mapString;\n'; + gFile += 'static const Map _$localeName = $mapString;\n'; } gFile +=