DPTW/Public/ecma/DPTW.I18N.ecma.js

72 lines
2.2 KiB
JavaScript
Executable File

DPTW.I18N = function(dptw, input){
const self = this,
sentences = {};
let started = false,
overwrite_add, default_text,
selected = null;
const construct = () => {};
this.start = callback => {
const end = status => typeof callback == "function" && callback(status);
if(started){
end(false);
return false;
};
started = true;
overwrite_add = dptw.settings(["i18n_overwrite", "overwrite"]);
selected = dptw.settings(["default_language", "language"]);
default_text = dptw.settings("default_text");
self.add(dptw.settings("i18n_files"), true, () => {
end(true);
});
return true;
};
this.add = (inputs, overwrite, callback) => dptw.load_blocks(
dptw.set_array(inputs),
json => {
typeof overwrite != "boolean" && (overwrite = overwrite_add);
for(const language in json){
!sentences[language] && (sentences[language] = {});
for(const key in json[language])
(overwrite || sentences[language][key] === undefined) && (sentences[language][key] = json[language][key]);
};
},
callback,
0
);
const get = keys => {
const l = (keys = (
keys instanceof Array ? keys :
typeof keys == "string" ? [keys] :
[]).filter((key, i, array) => typeof key == "string" && array.indexOf(key) == i)).length;
if(l){
const languages = [selected].concat(Object.keys(sentences)),
m = languages.length;
for(let j = 0; j < m; j ++)
if(languages[j] && sentences[languages[j]])
for(let i = 0; i < l; i ++)
if(keys[i] && sentences[languages[j]][keys[i]] !== undefined)
return sentences[languages[j]][keys[i]];
return keys[0];
};
return default_text;
};
this.get = (keys, variables) => dptw.string_variables(get(keys), variables);
construct();
};