generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
i18n.ts
43 lines (38 loc) · 1.19 KB
/
i18n.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
import Mustache from "mustache";
import { moment } from "obsidian";
import { LANGS } from "./langs";
export type LangType = keyof typeof LANGS;
export type LangTypeAndAuto = LangType | "auto";
export type TransItemType = keyof typeof LANGS["en"];
export class I18n {
lang: LangTypeAndAuto;
readonly saveSettingFunc: (tolang: LangTypeAndAuto) => Promise<void>;
constructor(
lang: LangTypeAndAuto,
saveSettingFunc: (tolang: LangTypeAndAuto) => Promise<void>
) {
this.lang = lang;
this.saveSettingFunc = saveSettingFunc;
}
async changeTo(anotherLang: LangTypeAndAuto) {
this.lang = anotherLang;
await this.saveSettingFunc(anotherLang);
}
_get(key: TransItemType) {
let realLang = this.lang;
if (this.lang === "auto" && moment.locale().replace("-", "_") in LANGS) {
realLang = moment.locale().replace("-", "_") as LangType;
} else {
realLang = "en";
}
const res: string =
(LANGS[realLang] as typeof LANGS["en"])[key] || LANGS["en"][key] || key;
return res;
}
t(key: TransItemType, vars?: Record<string, string>) {
if (vars === undefined) {
return this._get(key);
}
return Mustache.render(this._get(key), vars);
}
}