feat: add localized home routes

This commit is contained in:
TerryM
2026-06-01 15:09:58 +08:00
parent 9b08379d50
commit fa78568c94
8 changed files with 144 additions and 18 deletions

View File

@@ -5,6 +5,7 @@ import React, {
useMemo,
useState,
} from "react";
import { languageForHomePathname } from "./languageRoutes";
export type Lang = "zh-CN" | "en" | "ja" | "ko" | "vi" | "id" | "ms";
@@ -372,6 +373,10 @@ const LANG_KEY = "ark_lang";
export function I18nProvider({ children }: { children: React.ReactNode }) {
const [lang, setLangState] = useState<Lang>(() => {
const routeLang = languageForHomePathname(window.location.pathname);
if (routeLang) return routeLang;
if (window.location.pathname === "/") return "en";
const s = localStorage.getItem(LANG_KEY);
if (s === "zh" || s === "zh-TW") return "zh-CN";
if (
@@ -386,10 +391,10 @@ export function I18nProvider({ children }: { children: React.ReactNode }) {
return s;
return "en";
});
const setLang = (l: Lang) => {
const setLang = useCallback((l: Lang) => {
localStorage.setItem(LANG_KEY, l);
setLangState(l);
};
}, []);
const t = useCallback(
(k: string) => dict[lang][k] || dict.en[k] || k,
[lang],