Skip to content

Commit

Permalink
fix strict countryCode problem
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickChrestin committed Jun 12, 2024
1 parent e5d0f70 commit 149ef91
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
33 changes: 26 additions & 7 deletions lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Locale> supportedLocales, Locale? fallbackLocale) {
List<Locale> 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;
Expand Down Expand Up @@ -146,9 +161,11 @@ 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.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<AssetLoader> loaders = [
assetLoader,
Expand Down Expand Up @@ -207,9 +224,11 @@ class EasyLocalizationController extends ChangeNotifier {
Locale? get savedLocale => _savedLocale;

Future<void> 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);
}
}
Expand Down
58 changes: 46 additions & 12 deletions test/easy_localization_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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),
Expand All @@ -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'));
}));
});

Expand Down

0 comments on commit 149ef91

Please sign in to comment.