-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlocalize.ts
63 lines (55 loc) · 1.65 KB
/
localize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as cs from './translations/cs.json';
import * as da from './translations/da.json';
import * as de from './translations/de.json';
import * as en from './translations/en.json';
import * as fr from './translations/fr.json';
import * as hu from './translations/hu.json';
import * as it from './translations/it.json';
import * as lv from './translations/lv.json';
import * as nb from './translations/nb.json';
import * as nl from './translations/nl.json';
import * as pl from './translations/pl.json';
import * as ptBr from './translations/pt-br.json';
import * as sk from './translations/sk.json';
import * as sl from './translations/sl.json';
import * as sv from './translations/sv.json';
import type { HomeAssistant, LocalizeFunc } from './utils/ha';
const languages: Record<string, unknown> = {
cs,
da,
de,
en,
fr,
hu,
it,
lv,
nb,
nl,
pl,
// eslint-disable-next-line @typescript-eslint/naming-convention
'pt-BR': ptBr,
sk,
sl,
sv
};
// eslint-disable-next-line @typescript-eslint/naming-convention
const DEFAULT_LANG = 'en';
const getTranslatedString = (key: string, lang: string): string | undefined => {
try {
return key.
split('.').
reduce((prev, current) => (prev as Record<string, unknown>)[current], languages[lang]) as string;
} catch {
return undefined;
}
};
export default function setupCustomlocalize (hass?: HomeAssistant): LocalizeFunc {
return function (key: string) {
const lang = hass?.locale.language ?? DEFAULT_LANG;
let translated = getTranslatedString(key, lang);
if (!translated) {
translated = getTranslatedString(key, DEFAULT_LANG);
}
return translated ?? key;
};
}