-
Notifications
You must be signed in to change notification settings - Fork 0
/
LangClient.ts
146 lines (131 loc) · 4.05 KB
/
LangClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { CoreClient, ICoreKernel, ICoreKernelModule } from '@grandlinex/core';
import * as Path from 'path';
import * as fs from 'fs';
import { LangData } from '../lib/index.js';
import GLang from '../class/GLang.js';
import LangDb from '../db/LangDb.js';
import Language from '../db/entity/Language.js';
import Translation from '../db/entity/Translation.js';
export default class LangClient extends CoreClient<
ICoreKernel<any>,
LangDb,
LangClient,
null,
null
> {
static STORE_TRANSLATION_PATH = 'GLX_TRANSLATION_PATH';
static DEFAULT_LANG_DB_KEY = 'lang';
constructor(module: ICoreKernelModule<any, any, any, any, any>) {
super('lang-client', module);
}
async hasLang(code: string): Promise<boolean> {
return !!(await this.getModule().getDb().lang.getObjById(code));
}
async getLang(code: string): Promise<LangData | undefined> {
const db = this.getModule().getDb();
const lang = await db.lang.getObjById(code);
if (!lang) {
return undefined;
}
const d = await db.translations.getObjList({
search: {
t_lang: lang.e_id,
},
});
const nlang: LangData = {
label: lang.label,
code: lang.e_id,
data: d,
};
return nlang;
}
async getDefault(): Promise<LangData | undefined> {
return this.getLang(await this.getDbLang());
}
async getCur(): Promise<LangData | null> {
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);
if (lang) {
return lang;
}
}
}
return (await this.getDefault()) || null;
}
async getCurTranslator(): Promise<GLang> {
return new GLang(await this.getCur(), this);
}
async getLangList(): Promise<{ code: string; label: string }[]> {
return (await this.getModule().getDb().lang.getObjList()).map(
({ e_id, label }) => ({
code: e_id,
label,
})
);
}
async loadLangFromFolder(path: string) {
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({ 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({
key,
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: string): Promise<void> {
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(): Promise<string> {
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';
}
}