UNPKG

@grandlinex/bundle-multilang

Version:
38 lines (37 loc) 890 B
import { CMap } from '@grandlinex/core'; export default class GLang { constructor(langDat, log) { this.log = log; this.code = ''; this.map = new 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; } }