/
i18n.js
129 lines (116 loc) · 4.86 KB
/
i18n.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Copyright 2018 Shift Devices AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import i18n from 'i18next';
import appTranslationsDE from '../locales/de/app.json';
import appTranslationsEN from '../locales/en/app.json';
import appTranslationsFR from '../locales/fr/app.json';
import appTranslationsJA from '../locales/ja/app.json';
import appTranslationsRU from '../locales/ru/app.json';
import appTranslationsMS from '../locales/ms/app.json';
import appTranslationsPT from '../locales/pt/app.json';
import appTranslationsHI from '../locales/hi/app.json';
import appTranslationsBG from '../locales/bg/app.json';
import appTranslationsTR from '../locales/tr/app.json';
import appTranslationsZH from '../locales/zh/app.json';
import appTranslationsFA from '../locales/fa/app.json';
import appTranslationsES from '../locales/es/app.json';
import appTranslationsSL from '../locales/sl/app.json';
import appTranslationsHE from '../locales/he/app.json';
import languageFromConfig from './config';
import Backend from 'i18next-locize-backend';
import locizeEditor from 'locize-editor';
import { apiGet } from '../utils/request';
import { setConfig } from '../utils/config';
// if a language is not officially added yet, add it through this env var to make it available
// (e.g. "es,fr,sl").
/* eslint no-undef: "off" */
export const extraLanguages = process.env.PREACT_APP_I18N_ADDLANGUAGES;
/* eslint no-undef: "off" */
export const i18nEditorActive = process.env.PREACT_APP_I18NEDITOR === '1';
const locizeProjectID = 'fe4e5a24-e4a2-4903-96fc-3d62c11fc502';
let i18Init = i18n
.use(languageFromConfig);
if (i18nEditorActive) {
i18Init = i18Init
.use(Backend)
.use(locizeEditor);
}
i18Init.init({
fallbackLng: 'en',
// have a common namespace used around the full app
ns: ['app', 'wallet'],
defaultNS: 'app',
debug: false,
returnObjects: true,
interpolation: {
escapeValue: false // not needed for react
},
react: {
wait: true
},
backend: {
projectId: locizeProjectID,
referenceLng: 'en'
},
editor: {
enabled: i18nEditorActive,
autoOpen: true,
mode: 'iframe', // 'window',
projectId: locizeProjectID,
/* iframeContainerStyle: 'z-index: 2000; position: fixed; bottom: 0; right: 0; left: 0; height: 300px; box-shadow: -3px 0 5px 0 rgba(0,0,0,0.5);',
* iframeStyle: 'width: 100%; height: 300px; border: none;',
* bodyStyle: 'margin-bottom: 205px;', */
onEditorSaved: (lng, ns) => {
i18n.reloadResources(lng, ns);
}
},
});
if (!i18nEditorActive) {
i18n.addResourceBundle('de', 'app', appTranslationsDE);
i18n.addResourceBundle('en', 'app', appTranslationsEN);
i18n.addResourceBundle('fr', 'app', appTranslationsFR);
i18n.addResourceBundle('ja', 'app', appTranslationsJA);
i18n.addResourceBundle('ms', 'app', appTranslationsMS);
i18n.addResourceBundle('ru', 'app', appTranslationsRU);
i18n.addResourceBundle('pt', 'app', appTranslationsPT);
i18n.addResourceBundle('hi', 'app', appTranslationsHI);
i18n.addResourceBundle('bg', 'app', appTranslationsBG);
i18n.addResourceBundle('tr', 'app', appTranslationsTR);
i18n.addResourceBundle('zh', 'app', appTranslationsZH);
i18n.addResourceBundle('fa', 'app', appTranslationsFA);
i18n.addResourceBundle('es', 'app', appTranslationsES);
i18n.addResourceBundle('sl', 'app', appTranslationsSL);
i18n.addResourceBundle('he', 'app', appTranslationsHE);
}
i18n.on('languageChanged', (lng) => {
// Set userLanguage in config back to null if system locale matches
// the newly selected language lng to make the app use native-locale again.
// This also covers partial matches. For example, if native locale is pt_BR
// and the app has only pt translation, assume they match.
return apiGet('native-locale').then((nativeLocale) => {
let match = lng === nativeLocale;
if (!match) {
// There are too many combinations. So, we compare only the main
// language tag.
const lngLang = lng.replace('_', '-').split('-')[0];
const localeLang = nativeLocale.replace('_', '-').split('-')[0];
match = lngLang === localeLang;
}
const uiLang = match ? null : lng;
return setConfig({ frontend: { userLanguage: uiLang } });
});
});
export default i18n;