From 8065c22f1b5a19181e2604c5b9524ee8d2cc4608 Mon Sep 17 00:00:00 2001 From: Tien Do Nam Date: Tue, 6 Oct 2020 02:23:07 +0200 Subject: [PATCH] feat: prefer language code over region code --- CHANGELOG.md | 4 ++++ README.md | 2 +- example/README.md | 2 +- lib/fast_i18n.dart | 12 ++++++++---- pubspec.yaml | 2 +- test/fast_i18n_test.dart | 4 ++++ 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b61b187..acb79532 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.7.0 + +- Prefer language code over region code. + ## 1.6.1 - Add more unit tests. diff --git a/README.md b/README.md index 655eb85d..7af880e0 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Lightweight i18n solution. Use JSON files to create typesafe translations. ```yaml dependencies: - fast_i18n: ^1.6.1 + fast_i18n: ^1.7.0 dev_dependencies: build_runner: any diff --git a/example/README.md b/example/README.md index 1088f7c9..bdc3b6ef 100644 --- a/example/README.md +++ b/example/README.md @@ -4,7 +4,7 @@ ```yaml dependencies: - fast_i18n: ^1.6.1 + fast_i18n: ^1.7.0 dev_dependencies: build_runner: any diff --git a/lib/fast_i18n.dart b/lib/fast_i18n.dart index 6e28f3c4..0ae3a954 100644 --- a/lib/fast_i18n.dart +++ b/lib/fast_i18n.dart @@ -26,12 +26,16 @@ class FastI18n { orElse: () => null); if (selected != null) return selected; - // 2nd try: match the first or the second part + // 2nd try: match the first part (language) List deviceLocaleParts = candidate.split('-'); selected = supported.firstWhere( - (element) => - element == deviceLocaleParts.first || - element == deviceLocaleParts.last, + (element) => element == deviceLocaleParts.first, + orElse: () => null); + if (selected != null) return selected; + + // 3rd try: match the second part (region) + selected = supported.firstWhere( + (element) => element == deviceLocaleParts.last, orElse: () => null); if (selected != null) return selected; diff --git a/pubspec.yaml b/pubspec.yaml index 8891548b..eebf7cba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: fast_i18n description: Lightweight i18n solution. Use JSON files to create typesafe translations. -version: 1.6.1 +version: 1.7.0 homepage: https://github.com/Tienisto/flutter-fast-i18n environment: diff --git a/test/fast_i18n_test.dart b/test/fast_i18n_test.dart index 8f11f118..51613900 100644 --- a/test/fast_i18n_test.dart +++ b/test/fast_i18n_test.dart @@ -23,6 +23,10 @@ void testSelectLocale() { expect(FastI18n.selectLocale('de_DE', ['en', 'de']), 'de'); }); + test('prefer first part over second part', () { + expect(FastI18n.selectLocale('en_DE', ['de', 'en']), 'en'); + }); + test('match last part', () { expect(FastI18n.selectLocale('en_US', ['us', 'de']), 'us'); });