2018-02-05 12:58:17 +00:00
|
|
|
var yaml = require('js-yaml');
|
2016-10-15 18:58:30 +00:00
|
|
|
vnLocaleStrings = {};
|
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
|
|
|
{
|
2016-10-15 18:58:30 +00:00
|
|
|
language: null
|
2018-01-03 15:35:03 +00:00
|
|
|
,defaultLang: 'en'
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,init() {
|
2017-01-18 12:10:39 +00:00
|
|
|
if (this.language)
|
|
|
|
return;
|
|
|
|
|
2018-01-03 15:35:03 +00:00
|
|
|
var language = this.defaultLang;
|
2017-01-18 12:10:39 +00:00
|
|
|
var languages = navigator.languages;
|
|
|
|
|
|
|
|
if (languages && languages.length > 0)
|
|
|
|
language = languages[0];
|
|
|
|
else if (navigator.language)
|
|
|
|
language = navigator.language;
|
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
this.language = language.substr(0, 2);
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,load(path, callback) {
|
2022-11-11 19:31:17 +00:00
|
|
|
this.init();
|
2016-10-20 16:37:28 +00:00
|
|
|
|
2018-01-03 15:35:03 +00:00
|
|
|
var data = {
|
|
|
|
path: path,
|
|
|
|
callback: callback,
|
|
|
|
defOk: false,
|
|
|
|
orgOk: this.defaultLang === this.language
|
|
|
|
};
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
data.def = this.createRequest(data, true, this.defaultLang);
|
2018-01-03 15:35:03 +00:00
|
|
|
|
|
|
|
if (!data.orgOk)
|
2022-11-11 19:31:17 +00:00
|
|
|
data.org = this.createRequest(data, false, this.language);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2018-01-03 15:35:03 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,createRequest(data, isDef, lang) {
|
2022-11-11 19:31:17 +00:00
|
|
|
var langFile = data.path +'/locale/'+ lang +'.yml'+ Vn.getVersion();
|
2018-01-03 15:35:03 +00:00
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('get', langFile, true);
|
2018-01-03 15:35:03 +00:00
|
|
|
request.onreadystatechange =
|
2022-11-11 19:31:17 +00:00
|
|
|
this.onRequestReady.bind(this, request, data, isDef);
|
|
|
|
request.send();
|
2018-01-03 15:35:03 +00:00
|
|
|
return request;
|
2015-09-11 09:37:16 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,onRequestReady(request, data, isDef) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (request.readyState != 4)
|
|
|
|
return;
|
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
if (isDef) {
|
|
|
|
this.loadFromRequest(request);
|
2018-01-03 15:35:03 +00:00
|
|
|
data.defOk = true;
|
2022-11-11 19:31:17 +00:00
|
|
|
} else
|
2018-01-03 15:35:03 +00:00
|
|
|
data.orgOk = true;
|
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
if (data.orgOk && data.defOk) {
|
2018-01-03 15:35:03 +00:00
|
|
|
if (data.org != null)
|
2022-11-11 19:31:17 +00:00
|
|
|
this.loadFromRequest(data.org)
|
2018-01-03 15:35:03 +00:00
|
|
|
if (data.callback)
|
2022-11-11 19:31:17 +00:00
|
|
|
data.callback();
|
2018-01-03 15:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,loadFromRequest(request) {
|
2018-01-03 15:35:03 +00:00
|
|
|
if (request.status !== 200)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
try {
|
2022-11-11 19:31:17 +00:00
|
|
|
this.add(yaml.safeLoad(request.responseText));
|
2018-01-03 15:35:03 +00:00
|
|
|
return true;
|
2022-11-11 19:31:17 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
2018-01-03 15:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,loadScript(path, callback) {
|
2022-11-11 19:31:17 +00:00
|
|
|
this.init();
|
|
|
|
Vn.includeJs(path +'/locale/'+ this.language, callback);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,add(translations) {
|
2016-09-27 06:18:20 +00:00
|
|
|
for (var str in translations)
|
|
|
|
vnLocaleStrings[str] = translations[str];
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 19:31:17 +00:00
|
|
|
window._ = function(stringId) {
|
2016-09-26 09:28:47 +00:00
|
|
|
var string = vnLocaleStrings[stringId];
|
2018-01-03 15:35:03 +00:00
|
|
|
return string ? string : stringId;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|