stear/language.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-10-09 16:38:50 +02:00
export class LanguagePool {
#pool = {};
constructor() {
}
addFile(elems, lang) {
Object.entries(elems).forEach(([k, d]) => {
if (typeof this.#pool[k] == "undefined") {
this.#pool[k] = new LanguagePoolString();
}
this.#pool[k].set(d, lang);
});
}
set lang(lang) {
Object.entries(this.#pool).forEach(([k, d]) => {
d.lang = lang;
})
}
get getFile() {
var out = {};
Object.entries(this.#pool).forEach(([k, d]) => {
out[k] = d.get("default");
});
return out;
}
add(tag, defaultText = "") {
let newS = typeof this.#pool[tag] == "undefined" ? new LanguagePoolString() : this.#pool[tag];
this.#pool[tag] = newS;
newS.set(defaultText, "default");
return newS;
}
get(tag) {
return this.#pool[tag];
}
}
export class LanguagePoolString {
#value = {
};
#lang = "en";
constructor() { }
set(v, lang) {
this.#value[lang] = v;
}
get(lang) {
return this.#value[lang];
}
set lang(lang) {
this.#lang = lang;
}
r(...str) {
var out = this.#value[this.#lang] == null ? this.#value.default : this.#value[this.#lang];
for (let i = 0; i < str.length; i++) {
out = out.replace("{}", str[i]);
}
return out;
}
toString() {
return this.#value[this.#lang] == null ? this.#value.default : this.#value[this.#lang];
}
}