hedera-web/js/vn/locale.js

98 lines
2.1 KiB
JavaScript
Raw Normal View History

const yaml = require('js-yaml');
const locales = {};
let fallbackLang = 'en';
let lang = null;
2016-09-26 09:28:47 +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 =
{
init() {
if (lang) return;
this.loads = {};
this.locales = locales;
this.fallbackLang = fallbackLang;
const languages = navigator.languages;
if (languages && languages.length > 0)
lang = languages[0];
else if (navigator.language)
lang = navigator.language;
lang = lang ? lang.substr(0, 2) : fallbackLang;
this.language = lang;
},
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);
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) {
if (request.readyState != 4)
return;
if (request.status < 200 || request.status >= 400)
return reject(new Error(`HTTP ${request.status}: ${request.statusText}`));
resolve(yaml.safeLoad(request.responseText));
},
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];
}
}
function getString(stringId, lang) {
return locales[lang] ? locales[lang][stringId] : null;
}
window._ = function(stringId) {
let string = getString(stringId, lang);
if (!string)
string = getString(stringId, fallbackLang);
return string || stringId;
}