-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathi18n.js
More file actions
41 lines (35 loc) · 976 Bytes
/
Copy pathi18n.js
File metadata and controls
41 lines (35 loc) · 976 Bytes
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
import enStatic from './locales/en.js';
const _locales = { en: enStatic, ru: null };
const _pending = { ru: null };
const EMPTY = Object.freeze({});
function localeKey(lang) {
return lang === 'ru' ? 'ru' : 'en';
}
export async function ensureI18nLocales(...langs) {
const keys = [...new Set(langs.map(localeKey))];
await Promise.all(keys.filter((key) => key === 'ru').map((key) => loadLocale(key)));
}
async function loadLocale(key) {
if (key === 'en' || _locales[key]) {
return _locales[key];
}
if (!_pending[key]) {
_pending[key] = import('./locales/ru.js').then((mod) => {
_locales.ru = mod.default ?? mod;
return _locales.ru;
});
}
return _pending[key];
}
export function getTranslations(lang) {
const key = localeKey(lang);
return _locales[key] || _locales.en || EMPTY;
}
export const TRANSLATIONS = {
get en() {
return _locales.en || EMPTY;
},
get ru() {
return _locales.ru || _locales.en || EMPTY;
}
};