@grandlinex/bundle-multilang
Version:
> Multilang support for GrandLineX
175 lines (174 loc) • 6.43 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@grandlinex/core");
const Path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const GLang_js_1 = __importDefault(require("../class/GLang.js"));
const Language_js_1 = __importDefault(require("../db/entity/Language.js"));
const Translation_js_1 = __importDefault(require("../db/entity/Translation.js"));
class LangClient extends core_1.CoreClient {
constructor(module) {
super('lang-client', module);
}
async hasLang(code) {
return !!(await this.getModule().getDb().lang.getObjById(code));
}
async getAllTranslation() {
const db = this.getModule().getDb();
return db.translations.getObjList({
order: [
{
key: 'key',
order: 'ASC',
},
],
});
}
async getLang(code, scopes) {
const db = this.getModule().getDb();
const lang = await db.lang.getObjById(code);
if (!lang) {
return undefined;
}
let d = [];
if (!scopes) {
d = await db.translations.getObjList({
search: {
t_lang: lang.e_id,
},
});
}
else {
for (const scope of scopes) {
d = d.concat(await db.translations.getObjList({
search: {
t_lang: lang.e_id,
scope,
},
}));
}
}
const nlang = {
label: lang.label,
code: lang.e_id,
data: d,
};
return nlang;
}
async getDefault(scopes) {
return this.getLang(await this.getDbLang(), scopes);
}
async getCur(scopes) {
const db = this.getModule().getDb();
if (await db.configExist(LangClient.DEFAULT_LANG_DB_KEY)) {
const code = await db.getConfig(LangClient.DEFAULT_LANG_DB_KEY);
if (code && (await this.hasLang(code.c_value))) {
const lang = await this.getLang(code.c_value, scopes);
if (lang) {
return lang;
}
}
}
return (await this.getDefault()) || null;
}
async getCurTranslator(scopes) {
return new GLang_js_1.default(await this.getCur(scopes), this);
}
async getLangList() {
return (await this.getModule().getDb().lang.getObjList()).map(({ e_id, label }) => ({
code: e_id,
label,
}));
}
async loadLangFromFolder(path, scope = 'default') {
const info = fs.statSync(path);
const db = this.getModule().getDb();
this.debug(path);
if (info.isDirectory()) {
const el = fs.readdirSync(path, {
withFileTypes: true,
});
for (const item of el) {
this.debug(item.name);
if (item.isFile() && item.name.endsWith('.json')) {
const file = item.name.split('.')[0];
const name = file.split('-')[1];
const code = file.split('-')[0];
const langExist = await db.lang.getObjById(code);
if (langExist) {
this.log(`skip lang: ${code}-${name}`);
}
else {
const lang = await db.lang.createObject(new Language_js_1.default({ code, label: name }));
const stream = fs.readFileSync(Path.join(path, item.name), {
encoding: 'utf-8',
});
const json = JSON.parse(stream);
const keys = Object.keys(json);
for (const key of keys) {
await db.translations.createObject(new Translation_js_1.default({
key,
scope,
value: json[key],
t_lang: lang.e_id,
}));
}
this.log(`load lang: ${code}-${name}`);
}
}
}
}
else {
this.error('Target is not a foulder');
throw new Error('Target is not a foulder');
}
}
async setDbLang(lang) {
const db = this.getModule().getDb();
if ((await this.hasLang(lang)) && db) {
await db.setConfig(LangClient.DEFAULT_LANG_DB_KEY, lang);
}
else {
throw this.lError('Lang not exist');
}
}
async getDbLang() {
const db = this.getModule().getDb();
if (await db?.configExist(LangClient.DEFAULT_LANG_DB_KEY)) {
const code = await db?.getConfig(LangClient.DEFAULT_LANG_DB_KEY);
if (code) {
return code.c_value;
}
}
return 'en';
}
}
LangClient.STORE_TRANSLATION_PATH = 'GLX_TRANSLATION_PATH';
LangClient.DEFAULT_LANG_DB_KEY = 'lang';
exports.default = LangClient;