2022-11-17 22:31:48 +00:00
|
|
|
const yaml = require('js-yaml');
|
|
|
|
|
|
|
|
const locales = {};
|
|
|
|
let fallbackLang = 'en';
|
|
|
|
let lang = null;
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Class to manage the internationalization.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports =
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2022-11-17 22:31:48 +00:00
|
|
|
init() {
|
|
|
|
if (lang) return;
|
|
|
|
this.loads = {};
|
|
|
|
this.locales = locales;
|
|
|
|
this.fallbackLang = fallbackLang;
|
2017-01-18 12:10:39 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
const languages = navigator.languages;
|
2017-01-18 12:10:39 +00:00
|
|
|
|
|
|
|
if (languages && languages.length > 0)
|
2022-11-17 22:31:48 +00:00
|
|
|
lang = languages[0];
|
2017-01-18 12:10:39 +00:00
|
|
|
else if (navigator.language)
|
2022-11-17 22:31:48 +00:00
|
|
|
lang = navigator.language;
|
2016-10-20 16:37:28 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
lang = lang ? lang.substr(0, 2) : fallbackLang;
|
|
|
|
this.language = lang;
|
|
|
|
},
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
async load(path) {
|
|
|
|
this.init();
|
|
|
|
await Promise.all([
|
|
|
|
this.loadLang(path, fallbackLang),
|
|
|
|
this.loadLang(path, lang)
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
loadLang(path, lang) {
|
|
|
|
let langLoad = this.loads[lang];
|
|
|
|
if (!langLoad)
|
|
|
|
langLoad = this.loads[lang] = {};
|
|
|
|
|
|
|
|
if (langLoad[path])
|
|
|
|
return Promise.resolve();
|
|
|
|
langLoad[path] = true;
|
|
|
|
|
|
|
|
const langFile = `${path}/locale/${lang}.yml${Vn.getVersion()}`;
|
|
|
|
const request = new XMLHttpRequest();
|
2022-11-11 19:31:17 +00:00
|
|
|
request.open('get', langFile, true);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
return new Promise(
|
|
|
|
(resolve, reject) => {
|
|
|
|
request.onreadystatechange =
|
|
|
|
() => this.onRequestReady(request, resolve, reject);
|
|
|
|
request.send();
|
|
|
|
})
|
|
|
|
.then(translations => {
|
|
|
|
this.add(translations, lang);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
langLoad[path] = false;
|
|
|
|
console.warn(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onRequestReady(request, resolve, reject) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (request.readyState != 4)
|
|
|
|
return;
|
2022-11-17 22:31:48 +00:00
|
|
|
if (request.status < 200 || request.status >= 400)
|
|
|
|
return reject(new Error(`HTTP ${request.status}: ${request.statusText}`));
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
resolve(yaml.safeLoad(request.responseText));
|
|
|
|
},
|
2018-01-03 15:35:03 +00:00
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
add(translations, myLang) {
|
|
|
|
if (!translations) return;
|
|
|
|
myLang = myLang || lang;
|
|
|
|
let langLocales = locales[myLang];
|
|
|
|
if (!langLocales)
|
|
|
|
langLocales = locales[myLang] = {};
|
|
|
|
for (const str in translations)
|
|
|
|
langLocales[str] = translations[str];
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
function getString(stringId, lang) {
|
|
|
|
return locales[lang] ? locales[lang][stringId] : null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 22:31:48 +00:00
|
|
|
window._ = function(stringId) {
|
|
|
|
let string = getString(stringId, lang);
|
|
|
|
if (!string)
|
|
|
|
string = getString(stringId, fallbackLang);
|
|
|
|
return string || stringId;
|
|
|
|
}
|