feat: 服务器组件支持切换语言
This commit is contained in:
parent
3ca5a56127
commit
346cfa7ab3
|
|
@ -4,7 +4,7 @@ import { RightArrowIcon } from '@/assets/chatacter';
|
|||
import IconFont from '@/components/ui/iconFont';
|
||||
import ChatButton from './ChatButton';
|
||||
import Tags from '@/components/ui/Tags';
|
||||
import FormatText from '@/components/ui/format';
|
||||
import { useTranslations } from 'next-intl';
|
||||
|
||||
type CharacterBasicInfoProps = {
|
||||
characterId: string;
|
||||
|
|
@ -15,6 +15,8 @@ export default function CharacterBasicInfo({
|
|||
characterId,
|
||||
characterDetail,
|
||||
}: CharacterBasicInfoProps) {
|
||||
const t = useTranslations('common');
|
||||
|
||||
if (!characterDetail) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -115,7 +117,7 @@ export default function CharacterBasicInfo({
|
|||
width={18}
|
||||
height={20}
|
||||
/>
|
||||
<FormatText text="common.desc" />
|
||||
{t('desc')}
|
||||
</h2>
|
||||
<p className="text-text-color/60 mt-5 text-sm">
|
||||
{characterDetail.description}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
import { getTranslations } from 'next-intl/server';
|
||||
|
||||
type FormatTextProps = {
|
||||
text: string;
|
||||
values?: any;
|
||||
};
|
||||
|
||||
export default async function FormatText({ text, values }: FormatTextProps) {
|
||||
const t = await getTranslations();
|
||||
return t(text, values);
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
'use client';
|
||||
|
||||
import { NextIntlClientProvider } from 'next-intl';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import {
|
||||
createContext,
|
||||
useContext,
|
||||
|
|
@ -49,6 +50,7 @@ function getLocaleFromCookie(): Locale {
|
|||
export function IntlProvider({ children }: IntlProviderProps) {
|
||||
const [locale, setLocaleState] = useState<Locale>('en');
|
||||
const [messages, setMessages] = useState<Record<string, any>>();
|
||||
const router = useRouter();
|
||||
|
||||
const loadLocale = useMemoizedFn(async (locale: Locale) => {
|
||||
// 动态加载, 提升首屏加载速度
|
||||
|
|
@ -68,6 +70,7 @@ export function IntlProvider({ children }: IntlProviderProps) {
|
|||
setLocaleState(newLocale);
|
||||
setLocaleToCookie(newLocale);
|
||||
loadLocale(newLocale);
|
||||
router.refresh();
|
||||
});
|
||||
|
||||
if (!messages) return null;
|
||||
|
|
|
|||
Loading…
Reference in New Issue