feat: 服务器组件支持切换语言

This commit is contained in:
liuyonghe0111 2025-11-06 19:35:44 +08:00
parent 3ca5a56127
commit 346cfa7ab3
3 changed files with 7 additions and 13 deletions

View File

@ -4,7 +4,7 @@ import { RightArrowIcon } from '@/assets/chatacter';
import IconFont from '@/components/ui/iconFont'; import IconFont from '@/components/ui/iconFont';
import ChatButton from './ChatButton'; import ChatButton from './ChatButton';
import Tags from '@/components/ui/Tags'; import Tags from '@/components/ui/Tags';
import FormatText from '@/components/ui/format'; import { useTranslations } from 'next-intl';
type CharacterBasicInfoProps = { type CharacterBasicInfoProps = {
characterId: string; characterId: string;
@ -15,6 +15,8 @@ export default function CharacterBasicInfo({
characterId, characterId,
characterDetail, characterDetail,
}: CharacterBasicInfoProps) { }: CharacterBasicInfoProps) {
const t = useTranslations('common');
if (!characterDetail) { if (!characterDetail) {
return null; return null;
} }
@ -115,7 +117,7 @@ export default function CharacterBasicInfo({
width={18} width={18}
height={20} height={20}
/> />
<FormatText text="common.desc" /> {t('desc')}
</h2> </h2>
<p className="text-text-color/60 mt-5 text-sm"> <p className="text-text-color/60 mt-5 text-sm">
{characterDetail.description} {characterDetail.description}

View File

@ -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);
}

View File

@ -1,6 +1,7 @@
'use client'; 'use client';
import { NextIntlClientProvider } from 'next-intl'; import { NextIntlClientProvider } from 'next-intl';
import { useRouter } from 'next/navigation';
import { import {
createContext, createContext,
useContext, useContext,
@ -49,6 +50,7 @@ function getLocaleFromCookie(): Locale {
export function IntlProvider({ children }: IntlProviderProps) { export function IntlProvider({ children }: IntlProviderProps) {
const [locale, setLocaleState] = useState<Locale>('en'); const [locale, setLocaleState] = useState<Locale>('en');
const [messages, setMessages] = useState<Record<string, any>>(); const [messages, setMessages] = useState<Record<string, any>>();
const router = useRouter();
const loadLocale = useMemoizedFn(async (locale: Locale) => { const loadLocale = useMemoizedFn(async (locale: Locale) => {
// 动态加载, 提升首屏加载速度 // 动态加载, 提升首屏加载速度
@ -68,6 +70,7 @@ export function IntlProvider({ children }: IntlProviderProps) {
setLocaleState(newLocale); setLocaleState(newLocale);
setLocaleToCookie(newLocale); setLocaleToCookie(newLocale);
loadLocale(newLocale); loadLocale(newLocale);
router.refresh();
}); });
if (!messages) return null; if (!messages) return null;