SanYeCao-blog/src/i18n/index.ts

28 lines
544 B
TypeScript
Raw Normal View History

2026-03-24 19:32:44 +00:00
import zh from "./zh.ts";
import en from "./en.ts";
export const languages = {
zh,
en,
};
export type Lang = keyof typeof languages;
export function getLangFromUrl(url: URL): Lang {
const lang = url.pathname.split("/")[1];
if (lang === "en") return "en";
return "zh";
}
export function useTranslations(lang: Lang) {
return function t(path: string) {
const keys = path.split(".");
let current: any = languages[lang];
for (const key of keys) {
current = current?.[key];
}
return current ?? path;
};
}