commit 238db51e1fcd22e0b65d3176356ac328481f33ef
Author: fyears <1142836+fyears@users.noreply.github.com>
Date: Sun Mar 20 15:54:10 2022 +0800
add langs sub module
commit 30e8cad844821e62a1d6d1e36161c594ddf38111
Author: fyears <1142836+fyears@users.noreply.github.com>
Date: Sun Mar 20 15:30:45 2022 +0800
i18n
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
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);
|
|
}
|
|
}
|