mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-05-05 03:20:44 +08:00
feat: save locale in cookies
This commit is contained in:
41
frontend/src/core/i18n/context.tsx
Normal file
41
frontend/src/core/i18n/context.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
"use client";
|
||||
|
||||
import { createContext, useContext, useState, type ReactNode } from "react";
|
||||
|
||||
import type { Locale } from "@/core/i18n";
|
||||
|
||||
export interface I18nContextType {
|
||||
locale: Locale;
|
||||
setLocale: (locale: Locale) => void;
|
||||
}
|
||||
|
||||
export const I18nContext = createContext<I18nContextType | null>(null);
|
||||
|
||||
export function I18nProvider({
|
||||
children,
|
||||
initialLocale,
|
||||
}: {
|
||||
children: ReactNode;
|
||||
initialLocale: Locale;
|
||||
}) {
|
||||
const [locale, setLocale] = useState<Locale>(initialLocale);
|
||||
|
||||
const handleSetLocale = (newLocale: Locale) => {
|
||||
setLocale(newLocale);
|
||||
document.cookie = `locale=${newLocale}; path=/; max-age=31536000`;
|
||||
};
|
||||
|
||||
return (
|
||||
<I18nContext.Provider value={{ locale, setLocale: handleSetLocale }}>
|
||||
{children}
|
||||
</I18nContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useI18nContext() {
|
||||
const context = useContext(I18nContext);
|
||||
if (!context) {
|
||||
throw new Error("useI18n must be used within I18nProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user