Add i18n support

This commit is contained in:
2023-05-28 11:44:01 +08:00
parent 8f0ed03117
commit affe85f81c
11 changed files with 140 additions and 10 deletions

16
server/i18n.ts Normal file
View File

@@ -0,0 +1,16 @@
import { signal } from "@preact/signals";
export type I18NMap = { [x: string]: string | I18NMap };
export const i18n_map = signal<I18NMap>({});
const NOT_FOUND = "__NOT_FOUND__";
export default function t(key: string) {
const keys = key.split(".");
let map: string | I18NMap = i18n_map.value;
for (const k of keys) {
if (typeof map === "string") return NOT_FOUND;
else map = map[k];
}
return typeof map === "string" ? map : NOT_FOUND;
}