From 2bf349aa0d6cf9481c1df8d5eb2700596800aad3 Mon Sep 17 00:00:00 2001 From: Slouchwind <2241051890@qq.com> Date: Tue, 27 Jun 2023 13:49:21 +0800 Subject: [PATCH] locale --- components/i18n/index.ts | 8 +++++--- components/main.tsx | 4 ++-- components/students/studentsMethods.ts | 2 +- pages/chat.tsx | 6 +++--- pages/info.tsx | 4 ++-- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/components/i18n/index.ts b/components/i18n/index.ts index af8400b..156970b 100644 --- a/components/i18n/index.ts +++ b/components/i18n/index.ts @@ -1,3 +1,4 @@ +import { useEffect, useState } from "react"; import { useSetting } from "../setting"; export type Locales = 'zh-CN' | 'zh-TW'; @@ -11,14 +12,15 @@ export function fillBlank(i18n: string, ...fills: (string | undefined)[]): strin } export function useLocale(i18n: T) { + const [userLo, setUserLo] = useState('zh-CN'); + useEffect(() => setUserLo(window.navigator.language), []); const { setting } = useSetting(); - //const { locale: localeText, defaultLocale = 'zh-CN', ...other } = useRouter(); - const lo = setting.locale || 'zh-CN'; + const lo = setting.locale || userLo; function locale(key: K, loc?: string) { return (i18n as Record>)[key][loc || lo]; } function localeType(key: Type, loc?: string) { return (i18n as Record>)[key][loc || lo]; } - return { lo, locale, localeType }; + return { lo, locale, localeType, userLo }; } \ No newline at end of file diff --git a/components/main.tsx b/components/main.tsx index 2966749..94fe3b4 100644 --- a/components/main.tsx +++ b/components/main.tsx @@ -67,7 +67,7 @@ export default function MainNode({ children, onBodyClick }: { children: React.ReactNode; onBodyClick?: React.MouseEventHandler; }) { - const { lo, locale, localeType } = useLocale(main); + const { lo, locale, localeType, userLo } = useLocale(main); const { setting, setSetting } = useSetting({ locale: lo, @@ -120,7 +120,7 @@ export default function MainNode({ children, onBodyClick }: { display={display} /> )); - }, []); + }, [userLo]); return (
diff --git a/components/students/studentsMethods.ts b/components/students/studentsMethods.ts index 030a241..6409cf4 100644 --- a/components/students/studentsMethods.ts +++ b/components/students/studentsMethods.ts @@ -1,6 +1,6 @@ import { fileInfo, schaleInfo, studentsJson, studentInfo } from './students'; -export async function getStudentsJson(locale:string): Promise { +export async function getStudentsJson(locale: string): Promise { const fileJson: fileInfo[] = await fetch('../students.json').then(r => r.json()); const schaleJson: schaleInfo[] = await fetch(`https://schale.gg/data/${locale.slice(3).toLowerCase()}/students.min.json`).then(r => r.json()); return { fileJson, schaleJson }; diff --git a/pages/chat.tsx b/pages/chat.tsx index 22c238e..672a14b 100644 --- a/pages/chat.tsx +++ b/pages/chat.tsx @@ -239,7 +239,7 @@ interface SendMessageArg { } export default function Chat() { - const { lo, locale } = useLocale(chat); + const { lo, locale, userLo } = useLocale(chat); const [listState, setListState] = getClassState(useState({ student: 0, @@ -278,7 +278,7 @@ export default function Chat() { useEffect(() => { getStudentsJson(lo).then(r => setListState({ studentsJson: { data: r } })); - }, []); + }, [userLo]); //Window const { @@ -401,7 +401,7 @@ export default function Chat() { display={display} /> )); - }, []); + }, [userLo]); useEffect(() => { listState.studentsList?.forEach(id => { diff --git a/pages/info.tsx b/pages/info.tsx index 44420cd..e3e9318 100644 --- a/pages/info.tsx +++ b/pages/info.tsx @@ -57,7 +57,7 @@ interface State { } export default function Info() { - const { lo, locale } = useLocale(info); + const { lo, locale, userLo } = useLocale(info); const [state, setState] = getClassState(useState({ student: 0, studentsJson: { data: {} } @@ -65,7 +65,7 @@ export default function Info() { useEffect(() => { getStudentsJson(lo).then(r => setState({ studentsJson: { data: r } })); - }, []); + }, [userLo]); return (