aboutsummaryrefslogtreecommitdiffstats
path: root/src/helpers/i18n-helpers.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/helpers/i18n-helpers.ts')
-rw-r--r--src/helpers/i18n-helpers.ts72
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 @@
1export 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
31export 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}