@grandlinex/bundle-multilang
Version:
> Multilang support for GrandLineX
41 lines (40 loc) • 987 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@grandlinex/core");
class GLang {
constructor(langDat, log) {
this.log = log;
this.code = '';
this.map = new core_1.CMap();
this.missing = new Set();
if (langDat) {
this.loadLang(langDat);
}
}
clear() {
this.map.clear();
}
loadLang(lang) {
this.code = lang.code;
lang.data.forEach(({ key, value }) => {
this.map.set(key, value);
});
}
get(key) {
return this.translate(key);
}
translate(key) {
if (this.map.has(key)) {
return this.map.get(key) || '';
}
if (this.log) {
this.log.warn(`Missing translation: ${key}`);
}
else {
console.warn(`Missing translation: ${key}`);
}
this.missing.add(key);
return key;
}
}
exports.default = GLang;