UNPKG

@grandlinex/react-components

Version:
71 lines (70 loc) 1.97 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GLang = void 0; const BuildHelper_1 = __importDefault(require("./BuildHelper")); class GLang { constructor(langDat) { this.code = ''; this.map = new Map(); if (langDat) { this.loadLang(langDat); } const win = (0, BuildHelper_1.default)(); if (win && !win.missingTrans) { win.missingTrans = new Set(); win.getMissingTrans = () => { const missingTrans = win.missingTrans; const out = {}; missingTrans.forEach((val) => { out[val] = ''; }); console.log(out); }; } } getMissingSet() { const win = (0, BuildHelper_1.default)(); if (win && win.missingTrans) { return win.missingTrans; } return null; } clear() { this.map.clear(); } loadLang(lang) { this.code = lang.code; lang.data.forEach(({ key, value }) => { this.map.set(key, value); }); } loadDev(lang) { Object.keys(lang).forEach((key) => { this.map.set(key, lang[key]); }); } get(key) { return this.translate(key); } translate(key) { const set = this.getMissingSet(); if (this.map.has(key)) { if (set && set.has(key)) { set.delete(key); } return this.map.get(key) || ''; } if (set && !set.has(key)) { set.add(key); console.warn(`+Missing translation: ${key}`); } if (!set) { console.warn(`-Missing translation: ${key}`); } return key; } } exports.GLang = GLang;