-
Notifications
You must be signed in to change notification settings - Fork 97
/
lang.js
136 lines (111 loc) · 3.3 KB
/
lang.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
130
131
132
133
134
135
136
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import i18next from 'i18next';
import HttpBackend from 'i18next-http-backend';
import {initReactI18next} from 'react-i18next';
import logger from 'gmp/log';
import {isDefined} from 'gmp/utils/identity';
import {setLocale as setDateLocale} from './date';
import Detector from './detector';
import {getLanguageCodes} from './languages';
import {split} from 'gmp/utils/string';
const log = logger.getLogger('gmp.locale.lang');
let languageChangelisteners = [];
let currentLocale;
const notifyLanguageChangeListeners = (lang, initial = false) => {
for (const listener of languageChangelisteners) {
listener(lang, initial);
}
};
const fallbackLng = 'en';
const whitelist = getLanguageCodes();
const I18N_OPTIONS = {
storage: global.localStorage,
nsSeparator: false, // don't use a namespace separator in keys
keySeparator: false, // don't use a key separator in keys
fallbackLng,
ns: ['gsa'], // use gsa as namespace
defaultNS: 'gsa',
fallbackNS: 'gsa',
backend: {
loadPath: '/locales/{{ns}}-{{lng}}.json', // e.g. /locales/gsa-en.json
},
supportedLngs: whitelist,
nonExplicitSupportedLngs: false,
interpolation: {
skipOnVariables: false,
},
};
i18next.on('languageChanged', lang => {
if (currentLocale !== lang) {
log.debug('Language changed to', lang);
currentLocale = lang;
setDateLocale(lang);
notifyLanguageChangeListeners(lang);
}
});
export const initLocale = ({
backend = HttpBackend,
detector = Detector,
options = I18N_OPTIONS,
} = {}) =>
i18next.use(backend).use(detector).use(initReactI18next).init(options);
/**
* Subscribe to get notified about locale changes
*
* @param {Function} listener Function to get called when language changes
*
* @returns {Function} Unsubscribe function
*/
export const onLanguageChange = listener => {
languageChangelisteners.push(listener);
return () =>
(languageChangelisteners = languageChangelisteners.filter(
l => l !== listener,
));
};
/**
* Get the current used locale
*
* @returns {String} Language code of the current used language
*/
export const getLocale = () => currentLocale;
/**
* Change the current used locale
*
* @param {String} lang Language (code) to be set. Pass undefined
* to start automatic detection.
*/
export const setLocale = lang => {
if (isDefined(lang)) {
const code = lang.includes('-') ? split(lang, '-', 1)[0] : lang;
if (!whitelist.includes(lang) && !whitelist.includes(code)) {
log.warn(`Unknown locale ${lang}. Possible locales are ${whitelist}
Falling back to ${fallbackLng}.`);
lang = fallbackLng;
}
}
i18next.changeLanguage(lang, err => {
if (isDefined(err)) {
log.warn('Error while setting language to', lang, err);
}
});
};
class LazyTranslate {
constructor(key, options) {
this.key = key;
this.options = options;
}
toString() {
/* i18next-extract-disable-next-line */
return i18next.t(this.key, this.options);
}
}
/* i18next-extract-disable-next-line */
const translate = (key, options) => i18next.t(key, options);
const translateLazy = (key, options) => new LazyTranslate(key, options);
export {translate as _};
export {translateLazy as _l};
// vim: set ts=2 sw=2 tw=80: