-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (46 loc) · 1.41 KB
/
Copy pathindex.ts
File metadata and controls
56 lines (46 loc) · 1.41 KB
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
import ru from './locales/ru/translation';
import de from './locales/de/translation';
import nl from './locales/nl/translation';
import fr from './locales/fr/translation';
import en from './locales/en/translation';
import es from './locales/es/translation';
import it from './locales/it/translation';
import sk from './locales/sk/translation';
import hu from './locales/hu/translation';
import type { Translation, SupportedLanguage } from './types';
const translations: Record<SupportedLanguage, Translation> = {
en,
ru,
de,
nl,
fr,
es,
it,
sk,
hu
};
class I18n {
lang: SupportedLanguage = 'en';
fallback: SupportedLanguage = 'en';
t(key: string): string {
const path = key.split('.');
const fromCurrent = path.reduce<unknown>(
(o, k) => (o as Record<string, unknown>)?.[k],
translations[this.lang]
);
if (fromCurrent != null) return fromCurrent as string;
const fromFallback = path.reduce<unknown>(
(o, k) => (o as Record<string, unknown>)?.[k],
translations[this.fallback]
);
return (fromFallback as string) ?? key;
}
setLanguage(lang: string): void {
if (!translations[lang as SupportedLanguage] || this.lang === lang) return;
this.lang = lang as SupportedLanguage;
window.dispatchEvent(new CustomEvent('language-changed'));
}
}
export const i18n = new I18n();
(window as unknown as { i18n: I18n }).i18n = i18n;
export { translations };