visual-novel-web/src/layouts/Providers/IntlProvider.tsx

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-10-28 07:59:26 +00:00
'use client';
import { NextIntlClientProvider } from 'next-intl';
2025-11-05 11:32:23 +00:00
import {
createContext,
useContext,
useState,
useEffect,
ReactNode,
} from 'react';
import Cookies from 'js-cookie';
2025-11-06 06:15:32 +00:00
import { useMemoizedFn } from 'ahooks';
2025-10-28 07:59:26 +00:00
type Locale = 'zh' | 'en';
interface LocaleContextType {
locale: Locale;
setLocale: (locale: Locale) => void;
}
const LocaleContext = createContext<LocaleContextType | undefined>(undefined);
export function useLocale() {
const context = useContext(LocaleContext);
if (!context) {
throw new Error('useLocale must be used within IntlProvider');
}
return context;
}
interface IntlProviderProps {
children: ReactNode;
}
2025-11-05 11:32:23 +00:00
function setLocaleToCookie(locale: Locale) {
if (typeof window === 'undefined') return;
Cookies.set('locale', locale, { expires: 365, path: '/' });
}
function getLocaleFromCookie(): Locale {
if (typeof window === 'undefined') return 'en';
const cookieLocale = Cookies.get('locale') as Locale | undefined;
if (cookieLocale && (cookieLocale === 'zh' || cookieLocale === 'en')) {
return cookieLocale;
}
2025-11-06 09:47:54 +00:00
setLocaleToCookie('en');
2025-11-05 11:32:23 +00:00
return 'en';
}
2025-10-28 07:59:26 +00:00
export function IntlProvider({ children }: IntlProviderProps) {
2025-11-05 11:32:23 +00:00
const [locale, setLocaleState] = useState<Locale>('en');
2025-11-06 06:15:32 +00:00
const [messages, setMessages] = useState<Record<string, any>>();
const loadLocale = useMemoizedFn(async (locale: Locale) => {
// 动态加载, 提升首屏加载速度
const messages = await import(`@/locales/${locale}.ts`);
setMessages(messages.default);
});
2025-11-05 11:32:23 +00:00
useEffect(() => {
const cookieLocale = getLocaleFromCookie();
if (cookieLocale) {
2025-11-06 06:15:32 +00:00
loadLocale(cookieLocale);
2025-11-06 09:47:54 +00:00
setLocaleState(cookieLocale);
2025-11-05 11:32:23 +00:00
}
}, []);
2025-11-06 06:15:32 +00:00
const setLocale = useMemoizedFn((newLocale: Locale) => {
2025-11-05 11:32:23 +00:00
setLocaleState(newLocale);
setLocaleToCookie(newLocale);
2025-11-06 06:15:32 +00:00
loadLocale(newLocale);
});
if (!messages) return null;
2025-10-28 07:59:26 +00:00
return (
<LocaleContext.Provider value={{ locale, setLocale }}>
<NextIntlClientProvider
2025-11-05 11:32:23 +00:00
locale={locale as any}
2025-11-06 06:15:32 +00:00
messages={messages}
2025-11-06 09:47:54 +00:00
timeZone={Intl.DateTimeFormat().resolvedOptions().timeZone}
2025-10-28 07:59:26 +00:00
>
{children}
</NextIntlClientProvider>
</LocaleContext.Provider>
);
}