1 | import i18next from 'i18next';
|
2 |
|
3 | import {
|
4 | TRANSLATION_DE_DE,
|
5 | TRANSLATION_EN_US,
|
6 | TRANSLATION_ES_ES,
|
7 | TRANSLATION_FR_FR,
|
8 | TRANSLATION_HU_HU,
|
9 | TRANSLATION_IT_IT,
|
10 | TRANSLATION_JA_JP,
|
11 | TRANSLATION_KO_KR,
|
12 | TRANSLATION_NL_NL,
|
13 | TRANSLATION_PL_PL,
|
14 | TRANSLATION_PT_BR,
|
15 | TRANSLATION_SK_SK,
|
16 | TRANSLATION_ZH_CN,
|
17 | TRANSLATION_ZH_TW
|
18 | } from '../../locales';
|
19 |
|
20 | class I18nEngine {
|
21 | private static instance: I18nEngine;
|
22 | private constructor() {}
|
23 | public static getInstance() {
|
24 | if (!I18nEngine.instance) {
|
25 | I18nEngine.instance = new I18nEngine();
|
26 | }
|
27 | return I18nEngine.instance;
|
28 | }
|
29 |
|
30 | private availablesLanguages = {
|
31 | 'de-DE': 'de-DE',
|
32 | 'en-US': 'en-US',
|
33 | 'es-ES': 'es-ES',
|
34 | 'fr-FR': 'fr-FR',
|
35 | 'hu-HU': 'hu-HU',
|
36 | 'it-IT': 'it-IT',
|
37 | 'ja-JP': 'ja-JP',
|
38 | 'ko-KR': 'ko-KR',
|
39 | 'nl-NL': 'nl-NL',
|
40 | 'pl-PL': 'pl-PL',
|
41 | 'pt-BR': 'pt-BR',
|
42 | 'sk-SK': 'sk-SK',
|
43 | 'zh-CN': 'zh-CN',
|
44 | 'zh-TW': 'zh-TW'
|
45 | };
|
46 |
|
47 | public fallbackLanguage = 'en-US';
|
48 |
|
49 | public init(language: string) {
|
50 | i18next.init({
|
51 | lng: language,
|
52 | fallbackLng: this.fallbackLanguage,
|
53 | interpolation: {
|
54 | skipOnVariables: false
|
55 | }
|
56 | });
|
57 | i18next.addResources('de-DE', 'translation', TRANSLATION_DE_DE);
|
58 | i18next.addResources('en-US', 'translation', TRANSLATION_EN_US);
|
59 | i18next.addResources('es-ES', 'translation', TRANSLATION_ES_ES);
|
60 | i18next.addResources('fr-FR', 'translation', TRANSLATION_FR_FR);
|
61 | i18next.addResources('hu-HU', 'translation', TRANSLATION_HU_HU);
|
62 | i18next.addResources('it-IT', 'translation', TRANSLATION_IT_IT);
|
63 | i18next.addResources('ja-JP', 'translation', TRANSLATION_JA_JP);
|
64 | i18next.addResources('ko-KR', 'translation', TRANSLATION_KO_KR);
|
65 | i18next.addResources('nl-NL', 'translation', TRANSLATION_NL_NL);
|
66 | i18next.addResources('pl-PL', 'translation', TRANSLATION_PL_PL);
|
67 | i18next.addResources('pt-BR', 'translation', TRANSLATION_PT_BR);
|
68 | i18next.addResources('sk-SK', 'translation', TRANSLATION_SK_SK);
|
69 | i18next.addResources('zh-CN', 'translation', TRANSLATION_ZH_CN);
|
70 | i18next.addResources('zh-TW', 'translation', TRANSLATION_ZH_TW);
|
71 | }
|
72 |
|
73 | public translate(key: string): string {
|
74 | return i18next.t(key);
|
75 | }
|
76 |
|
77 | public exists(key: string): boolean {
|
78 | return i18next.exists(key);
|
79 | }
|
80 |
|
81 | public supportLanguage(language: string): boolean {
|
82 | return typeof this.availablesLanguages[language] !== 'undefined';
|
83 | }
|
84 | }
|
85 |
|
86 | export default I18nEngine.getInstance();
|