Skip to content

Commit

Permalink
feat: prefer language code over region code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Oct 6, 2020
1 parent ff4ff22 commit 8065c22
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.7.0

- Prefer language code over region code.

## 1.6.1

- Add more unit tests.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```yaml
dependencies:
fast_i18n: ^1.6.1
fast_i18n: ^1.7.0

dev_dependencies:
build_runner: any
Expand Down
12 changes: 8 additions & 4 deletions lib/fast_i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 4 additions & 0 deletions test/fast_i18n_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down

0 comments on commit 8065c22

Please sign in to comment.