Skip to content

Commit

Permalink
locale
Browse files Browse the repository at this point in the history
  • Loading branch information
Slouchwind committed Jun 27, 2023
1 parent e0c99fc commit 2bf349a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions components/i18n/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from "react";
import { useSetting } from "../setting";

export type Locales = 'zh-CN' | 'zh-TW';
Expand All @@ -11,14 +12,15 @@ export function fillBlank(i18n: string, ...fills: (string | undefined)[]): strin
}

export function useLocale<T extends i18nContents, K extends keyof T>(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<K, Record<string, string>>)[key][loc || lo];
}
function localeType<Type extends string>(key: Type, loc?: string) {
return (i18n as Record<Type, Record<string, string>>)[key][loc || lo];
}
return { lo, locale, localeType };
return { lo, locale, localeType, userLo };
}
4 changes: 2 additions & 2 deletions components/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -120,7 +120,7 @@ export default function MainNode({ children, onBodyClick }: {
display={display}
/>
));
}, []);
}, [userLo]);

return (
<div id={styles.main} onClick={onBodyClick}>
Expand Down
2 changes: 1 addition & 1 deletion components/students/studentsMethods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileInfo, schaleInfo, studentsJson, studentInfo } from './students';

export async function getStudentsJson(locale:string): Promise<studentsJson> {
export async function getStudentsJson(locale: string): Promise<studentsJson> {
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 };
Expand Down
6 changes: 3 additions & 3 deletions pages/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ListState>({
student: 0,
Expand Down Expand Up @@ -278,7 +278,7 @@ export default function Chat() {

useEffect(() => {
getStudentsJson(lo).then(r => setListState({ studentsJson: { data: r } }));
}, []);
}, [userLo]);

//Window
const {
Expand Down Expand Up @@ -401,7 +401,7 @@ export default function Chat() {
display={display}
/>
));
}, []);
}, [userLo]);

useEffect(() => {
listState.studentsList?.forEach(id => {
Expand Down
4 changes: 2 additions & 2 deletions pages/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ interface State {
}

export default function Info() {
const { lo, locale } = useLocale(info);
const { lo, locale, userLo } = useLocale(info);
const [state, setState] = getClassState(useState<State>({
student: 0,
studentsJson: { data: {} }
}));

useEffect(() => {
getStudentsJson(lo).then(r => setState({ studentsJson: { data: r } }));
}, []);
}, [userLo]);

return (
<MainNode>
Expand Down

1 comment on commit 2bf349a

@vercel
Copy link

@vercel vercel bot commented on 2bf349a Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.