feat: close #2618 use correct html lang attr

This commit is contained in:
Yidadaa 2023-08-14 21:55:18 +08:00
parent ae8226907f
commit e8e01aa60d
3 changed files with 24 additions and 2 deletions

View File

@ -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<boolean>(false);
@ -168,6 +179,7 @@ export function useLoadData() {
export function Home() {
useSwitchTheme();
useLoadData();
useHtmlLang();
useEffect(() => {
console.log("[Config] got config from build time", getClientConfig());

View File

@ -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",

View File

@ -116,3 +116,13 @@ export function changeLang(lang: Lang) {
setItem(LANG_KEY, lang);
location.reload();
}
export function getISOLang() {
const isoLangString: Record<string, string> = {
cn: "zh-Hans",
tw: "zh-Hant",
};
const lang = getLang();
return isoLangString[lang] ?? lang;
}