diff --git a/app/components/home.tsx b/app/components/home.tsx index c6829c2d..745298d5 100644 --- a/app/components/home.tsx +++ b/app/components/home.tsx @@ -15,7 +15,7 @@ import dynamic from "next/dynamic"; import { Path, SlotID } from "../constant"; import { ErrorBoundary } from "./error"; -import { getLang } from "../locales"; +import { getISOLang, getLang } from "../locales"; import { HashRouter as Router, @@ -86,6 +86,17 @@ export function useSwitchTheme() { }, [config.theme]); } +function useHtmlLang() { + useEffect(() => { + const lang = getISOLang(); + const htmlLang = document.documentElement.lang; + + if (lang !== htmlLang) { + document.documentElement.lang = lang; + } + }, []); +} + const useHasHydrated = () => { const [hasHydrated, setHasHydrated] = useState(false); @@ -168,6 +179,7 @@ export function useLoadData() { export function Home() { useSwitchTheme(); useLoadData(); + useHtmlLang(); useEffect(() => { console.log("[Config] got config from build time", getClientConfig()); diff --git a/app/layout.tsx b/app/layout.tsx index 883a268d..5e076265 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -3,7 +3,7 @@ import "./styles/globals.scss"; import "./styles/markdown.scss"; import "./styles/highlight.scss"; import { getClientConfig } from "./config/client"; -import { type Metadata } from 'next'; +import { type Metadata } from "next"; export const metadata: Metadata = { title: "ChatGPT Next Web", diff --git a/app/locales/index.ts b/app/locales/index.ts index 7ece4583..528600be 100644 --- a/app/locales/index.ts +++ b/app/locales/index.ts @@ -116,3 +116,13 @@ export function changeLang(lang: Lang) { setItem(LANG_KEY, lang); location.reload(); } + +export function getISOLang() { + const isoLangString: Record = { + cn: "zh-Hans", + tw: "zh-Hant", + }; + + const lang = getLang(); + return isoLangString[lang] ?? lang; +}