fix: i18n change

This commit is contained in:
archer
2023-07-25 22:18:18 +08:00
parent b367082d38
commit 2b993b926a
8 changed files with 58 additions and 31 deletions

View File

@@ -1,8 +1,8 @@
import React, { useState } from 'react';
import { Menu, MenuButton, MenuItem, MenuList, MenuButtonProps } from '@chakra-ui/react';
import { useRouter } from 'next/router';
import { getLangStore, LangEnum, setLangStore } from '@/utils/i18n';
import MyIcon from '@/components/Icon';
import { useTranslation } from 'react-i18next';
const langMap = {
[LangEnum.en]: {
@@ -16,7 +16,8 @@ const langMap = {
};
const Language = (props: MenuButtonProps) => {
const router = useRouter();
const { i18n } = useTranslation();
const [language, setLanguage] = useState<`${LangEnum}`>(getLangStore());
return (
@@ -41,12 +42,7 @@ const Language = (props: MenuButtonProps) => {
const lang = key as `${LangEnum}`;
setLangStore(lang);
setLanguage(lang);
router.replace({
query: {
...router.query,
lang
}
});
i18n?.changeLanguage?.(lang);
}}
>
{lang.label}

View File

@@ -0,0 +1,29 @@
import React from 'react';
import {
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalCloseButton,
ModalProps
} from '@chakra-ui/react';
interface Props extends ModalProps {
showCloseBtn?: boolean;
title?: string;
}
const MyModal = ({ isOpen, onClose, title, children, showCloseBtn = true, ...props }: Props) => {
return (
<Modal isOpen={isOpen} onClose={onClose} autoFocus={false} {...props}>
<ModalOverlay />
<ModalContent>
{!!title && <ModalHeader>{title}</ModalHeader>}
{showCloseBtn && <ModalCloseButton />}
{children}
</ModalContent>
</Modal>
);
};
export default MyModal;

View File

@@ -9,8 +9,9 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import NProgress from 'nprogress'; //nprogress module
import Router from 'next/router';
import { clientInitData, feConfigs } from '@/store/static';
import { appWithTranslation } from 'next-i18next';
import { setLangStore } from '@/utils/i18n';
import { appWithTranslation, useTranslation } from 'next-i18next';
import { getLangStore, setLangStore } from '@/utils/i18n';
import { useRouter } from 'next/router';
import 'nprogress/nprogress.css';
import '@/styles/reset.scss';
@@ -32,6 +33,9 @@ const queryClient = new QueryClient({
});
function App({ Component, pageProps }: AppProps) {
const router = useRouter();
const { i18n } = useTranslation();
const [googleClientVerKey, setGoogleVerKey] = useState<string>();
const [baiduTongji, setBaiduTongji] = useState<string>();
@@ -44,9 +48,14 @@ function App({ Component, pageProps }: AppProps) {
setBaiduTongji(baiduTongji);
})();
setLangStore('en');
setLangStore('zh');
}, []);
useEffect(() => {
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [router.asPath]);
return (
<>
<Head>

View File

@@ -1,17 +0,0 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import type { ChatModelItemType } from '@/constants/model';
import { ChatModelMap, OpenAiChatEnum } from '@/constants/model';
// get the models available to the system
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const chatModelList: ChatModelItemType[] = [];
chatModelList.push(ChatModelMap[OpenAiChatEnum.GPT3516k]);
chatModelList.push(ChatModelMap[OpenAiChatEnum.GPT35]);
chatModelList.push(ChatModelMap[OpenAiChatEnum.GPT4]);
jsonRes(res, {
data: chatModelList
});
}

View File

@@ -28,6 +28,7 @@ import SliderApps from './components/SliderApps';
import ChatHeader from './components/ChatHeader';
import { getErrText } from '@/utils/tools';
import { useUserStore } from '@/store/user';
import { serviceSideProps } from '@/utils/i18n';
const Chat = ({ appId, chatId }: { appId: string; chatId: string }) => {
const router = useRouter();
@@ -338,7 +339,8 @@ export async function getServerSideProps(context: any) {
return {
props: {
appId: context?.query?.appId || '',
chatId: context?.query?.chatId || ''
chatId: context?.query?.chatId || '',
...(await serviceSideProps(context))
}
};
}

View File

@@ -18,6 +18,7 @@ import ChatBox, { type ComponentRef, type StartChatFnProps } from '@/components/
import PageContainer from '@/components/PageContainer';
import ChatHeader from './components/ChatHeader';
import ChatHistorySlider from './components/ChatHistorySlider';
import { serviceSideProps } from '@/utils/i18n';
const ShareChat = ({ shareId, chatId }: { shareId: string; chatId: string }) => {
const router = useRouter();
@@ -232,7 +233,7 @@ export async function getServerSideProps(context: any) {
const chatId = context?.query?.chatId || '';
return {
props: { shareId, chatId }
props: { shareId, chatId, ...(await serviceSideProps(context)) }
};
}

View File

@@ -160,7 +160,7 @@ export async function getServerSideProps(context: any) {
const kbId = context?.query?.kbId;
return {
props: { currentTab, kbId, ...(await serviceSideProps(content)) }
props: { currentTab, kbId, ...(await serviceSideProps(context)) }
};
}

View File

@@ -9,6 +9,7 @@ import { useUserStore } from '@/store/user';
import { useChatStore } from '@/store/chat';
import LoginForm from './components/LoginForm';
import dynamic from 'next/dynamic';
import { serviceSideProps } from '@/utils/i18n';
const RegisterForm = dynamic(() => import('./components/RegisterForm'));
const ForgetPasswordForm = dynamic(() => import('./components/ForgetPasswordForm'));
@@ -97,4 +98,10 @@ const Login = () => {
);
};
export async function getServerSideProps(context: any) {
return {
props: { ...(await serviceSideProps(context)) }
};
}
export default Login;