diff options
author | 2021-08-15 17:15:09 +0530 | |
---|---|---|
committer | 2021-08-15 13:54:00 +0000 | |
commit | bc76d19c6f5687dd18c96db249e0abe7ad79a673 (patch) | |
tree | d4e977f99be77d723ce93bb3866468d8b63ec038 /src/helpers/i18n-helpers.ts | |
parent | chore: npm upgrades (diff) | |
download | ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.tar.gz ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.tar.zst ferdium-app-bc76d19c6f5687dd18c96db249e0abe7ad79a673.zip |
chore: typescript conversion of some minor utilities
Also removed 'targz' unused package from runtime.
Diffstat (limited to 'src/helpers/i18n-helpers.ts')
-rw-r--r-- | src/helpers/i18n-helpers.ts | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts new file mode 100644 index 000000000..c1f18f446 --- /dev/null +++ b/src/helpers/i18n-helpers.ts | |||
@@ -0,0 +1,72 @@ | |||
1 | export function getLocale({ | ||
2 | locale, locales, defaultLocale, fallbackLocale, | ||
3 | }) { | ||
4 | let localeStr = locale; | ||
5 | if (locales[locale] === undefined) { | ||
6 | let localeFuzzy: string | undefined; | ||
7 | Object.keys(locales).forEach((localStr) => { | ||
8 | if (locales && Object.hasOwnProperty.call(locales, localStr)) { | ||
9 | if (locale.substring(0, 2) === localStr.substring(0, 2)) { | ||
10 | localeFuzzy = localStr; | ||
11 | } | ||
12 | } | ||
13 | }); | ||
14 | |||
15 | if (localeFuzzy !== undefined) { | ||
16 | localeStr = localeFuzzy; | ||
17 | } | ||
18 | } | ||
19 | |||
20 | if (locales[localeStr] === undefined) { | ||
21 | localeStr = defaultLocale; | ||
22 | } | ||
23 | |||
24 | if (!localeStr) { | ||
25 | localeStr = fallbackLocale; | ||
26 | } | ||
27 | |||
28 | return localeStr; | ||
29 | } | ||
30 | |||
31 | export function getSelectOptions({ | ||
32 | locales, resetToDefaultText = '', automaticDetectionText = '', sort = true, | ||
33 | }) { | ||
34 | const options: object[] = []; | ||
35 | |||
36 | if (resetToDefaultText) { | ||
37 | options.push( | ||
38 | { | ||
39 | value: '', | ||
40 | label: resetToDefaultText, | ||
41 | }, | ||
42 | ); | ||
43 | } | ||
44 | |||
45 | if (automaticDetectionText) { | ||
46 | options.push( | ||
47 | { | ||
48 | value: 'automatic', | ||
49 | label: automaticDetectionText, | ||
50 | }, | ||
51 | ); | ||
52 | } | ||
53 | |||
54 | options.push({ | ||
55 | value: '───', | ||
56 | label: '───', | ||
57 | disabled: true, | ||
58 | }); | ||
59 | |||
60 | let keys = Object.keys(locales); | ||
61 | if (sort) { | ||
62 | keys = keys.sort(Intl.Collator().compare); | ||
63 | } | ||
64 | keys.forEach((key) => { | ||
65 | options.push({ | ||
66 | value: key, | ||
67 | label: locales[key], | ||
68 | }); | ||
69 | }); | ||
70 | |||
71 | return options; | ||
72 | } | ||