Skip to content

Commit

Permalink
[locale] compare scriptCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-seongwoo-jun authored and aissat committed Apr 23, 2022
1 parent 8e0e1b7 commit 966d5b2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 13 deletions.
54 changes: 41 additions & 13 deletions lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,29 @@ class EasyLocalizationController extends ChangeNotifier {
_locale = _savedLocale!;
} else {
// From Device Locale
_locale = supportedLocales.firstWhere(
(locale) => _checkInitLocale(locale, _deviceLocale),
orElse: () => _getFallbackLocale(supportedLocales, fallbackLocale));
_locale = selectLocaleFrom(
supportedLocales,
_deviceLocale,
fallbackLocale: fallbackLocale,
);
}
}

@visibleForTesting
static Locale selectLocaleFrom(
List<Locale> supportedLocales,
Locale deviceLocale, {
Locale? fallbackLocale,
}) {
final selectedLocale = supportedLocales.firstWhere(
(locale) => locale.supports(deviceLocale),
orElse: () => _getFallbackLocale(supportedLocales, fallbackLocale),
);
return selectedLocale;
}

//Get fallback Locale
Locale _getFallbackLocale(
static Locale _getFallbackLocale(
List<Locale> supportedLocales, Locale? fallbackLocale) {
//If fallbackLocale not set then return first from supportedLocales
if (fallbackLocale != null) {
Expand All @@ -65,15 +80,6 @@ class EasyLocalizationController extends ChangeNotifier {
}
}

bool _checkInitLocale(Locale locale, Locale? _deviceLocale) {
// If supported locale not contain countryCode then check only languageCode
if (locale.countryCode == null) {
return (locale.languageCode == _deviceLocale!.languageCode);
} else {
return (locale == _deviceLocale);
}
}

Future loadTranslations() async {
Map<String, dynamic> data;
try {
Expand Down Expand Up @@ -156,3 +162,25 @@ class EasyLocalizationController extends ChangeNotifier {
await setLocale(_deviceLocale);
}
}

@visibleForTesting
extension LocaleExtension on Locale {
bool supports(Locale locale) {
if (this == locale) {
return true;
}
if (languageCode != locale.languageCode) {
return false;
}
if (countryCode != null &&
countryCode!.isNotEmpty &&
countryCode != locale.countryCode) {
return false;
}
if (scriptCode != null && scriptCode != locale.scriptCode) {
return false;
}

return true;
}
}
50 changes: 50 additions & 0 deletions test/easy_localization_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,56 @@ void main() {
expect(Localization.instance.tr('path'), 'path/en-us.json');
});

group('locale', () {
test('locale supports device locale', () {
const en = Locale('en');
const en2 = Locale('en', '');
const enUS = Locale('en', 'US');
const enGB = Locale('en', 'GB');
expect(en.supports(enUS), isTrue);
expect(en2.supports(enUS), isTrue);
expect(enUS.supports(enUS), isTrue);
expect(enGB.supports(enUS), isFalse);

const zh = Locale('zh', '');
const zh2 = Locale('zh', '');
const zhCN = Locale('zh', 'CN');
const zhHans =
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans');
const zhHant =
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant');
const zhHansCN = Locale.fromSubtags(
languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN');
expect(zh.supports(zhHansCN), isTrue);
expect(zh2.supports(zhHansCN), isTrue);
expect(zhCN.supports(zhHansCN), isTrue);
expect(zhHans.supports(zhHansCN), isTrue);
expect(zhHant.supports(zhHansCN), isFalse);
expect(zhHansCN.supports(zhHansCN), isTrue);
});

test('select locale from device locale', () {
const en = Locale('en', '');
const zh = Locale('zh', '');
const zhHans =
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans');
const zhHant =
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant');
const zhHansCN = Locale.fromSubtags(
languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN');

expect(
EasyLocalizationController.selectLocaleFrom([en, zh], zhHansCN),
zh,
);
expect(
EasyLocalizationController.selectLocaleFrom(
[zhHant, zhHans], zhHansCN),
zhHans,
);
});
});

group('tr', () {
var r = EasyLocalizationController(
forceLocale: Locale('en'),
Expand Down

0 comments on commit 966d5b2

Please sign in to comment.