diff options
author | 2021-10-07 08:04:11 +0200 | |
---|---|---|
committer | 2021-10-07 08:04:11 +0200 | |
commit | 4898ff4a6a7bfc83a10ddc2f32abb2da82f4819f (patch) | |
tree | f17e2b06ab8c8c49ff428134a433e957b26ac0c0 /src/helpers/i18n-helpers.ts | |
parent | chore: upgrade react to latest (#2030) (diff) | |
download | ferdium-app-4898ff4a6a7bfc83a10ddc2f32abb2da82f4819f.tar.gz ferdium-app-4898ff4a6a7bfc83a10ddc2f32abb2da82f4819f.tar.zst ferdium-app-4898ff4a6a7bfc83a10ddc2f32abb2da82f4819f.zip |
refactor: locale selection cleanup (#2031)
Diffstat (limited to 'src/helpers/i18n-helpers.ts')
-rw-r--r-- | src/helpers/i18n-helpers.ts | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts index ec7dc8e98..e6c66c7d3 100644 --- a/src/helpers/i18n-helpers.ts +++ b/src/helpers/i18n-helpers.ts | |||
@@ -1,52 +1,44 @@ | |||
1 | export function getLocale({ | 1 | export function getLocale({ locale, locales, fallbackLocale }) { |
2 | locale, locales, defaultLocale, fallbackLocale, | 2 | if (!locale) { |
3 | }) { | 3 | return fallbackLocale; |
4 | let localeStr = locale; | 4 | } |
5 | if (locales[locale] === undefined) { | 5 | |
6 | if (!locales[locale]) { | ||
6 | let localeFuzzy: string | undefined; | 7 | let localeFuzzy: string | undefined; |
7 | for (const localStr of Object.keys(locales)) { | 8 | for (const localStr of Object.keys(locales)) { |
8 | if (locales && Object.hasOwnProperty.call(locales, localStr) && locale.slice(0, 2) === localStr.slice(0, 2)) { | 9 | if (locale.slice(0, 2) === localStr.slice(0, 2)) { |
9 | localeFuzzy = localStr; | 10 | localeFuzzy = localStr; |
10 | } | 11 | } |
11 | } | 12 | } |
12 | 13 | ||
13 | if (localeFuzzy !== undefined) { | 14 | if (localeFuzzy) { |
14 | localeStr = localeFuzzy; | 15 | return localeFuzzy; |
15 | } | 16 | } |
16 | } | 17 | } |
17 | 18 | ||
18 | if (locales[localeStr] === undefined) { | 19 | return locale; |
19 | localeStr = defaultLocale; | ||
20 | } | ||
21 | |||
22 | if (!localeStr) { | ||
23 | localeStr = fallbackLocale; | ||
24 | } | ||
25 | |||
26 | return localeStr; | ||
27 | } | 20 | } |
28 | 21 | ||
29 | export function getSelectOptions({ | 22 | export function getSelectOptions({ |
30 | locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, | 23 | locales, |
24 | resetToDefaultText = '', | ||
25 | automaticDetectionText = '', | ||
26 | sort = true, | ||
31 | }) { | 27 | }) { |
32 | const options: object[] = []; | 28 | const options: object[] = []; |
33 | 29 | ||
34 | if (resetToDefaultText) { | 30 | if (resetToDefaultText) { |
35 | options.push( | 31 | options.push({ |
36 | { | 32 | value: '', |
37 | value: '', | 33 | label: resetToDefaultText, |
38 | label: resetToDefaultText, | 34 | }); |
39 | }, | ||
40 | ); | ||
41 | } | 35 | } |
42 | 36 | ||
43 | if (automaticDetectionText) { | 37 | if (automaticDetectionText) { |
44 | options.push( | 38 | options.push({ |
45 | { | 39 | value: 'automatic', |
46 | value: 'automatic', | 40 | label: automaticDetectionText, |
47 | label: automaticDetectionText, | 41 | }); |
48 | }, | ||
49 | ); | ||
50 | } | 42 | } |
51 | 43 | ||
52 | options.push({ | 44 | options.push({ |