-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLang.ts
52 lines (41 loc) · 1.01 KB
/
GLang.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
import { CMap, CoreLogChannel } from '@grandlinex/core';
import { LangData } from '../lib/index.js';
export default class GLang {
code: string;
map: Map<string, string>;
missing: Set<string>;
log?: CoreLogChannel;
constructor(langDat: LangData | null, log?: CoreLogChannel) {
this.log = log;
this.code = '';
this.map = new CMap<string, string>();
this.missing = new Set<string>();
if (langDat) {
this.loadLang(langDat);
}
}
clear() {
this.map.clear();
}
loadLang(lang: LangData): void {
this.code = lang.code;
lang.data.forEach(({ key, value }) => {
this.map.set(key, value);
});
}
get(key: string): string {
return this.translate(key);
}
private translate(key: string): string {
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;
}
}