Files
deer-flow/frontend/src/core/i18n/hooks.ts

43 lines
946 B
TypeScript
Raw Normal View History

2026-01-20 14:06:47 +08:00
"use client";
2026-01-20 16:00:39 +08:00
import { useEffect } from "react";
2026-01-20 14:06:47 +08:00
2026-01-20 16:00:39 +08:00
import { useI18nContext } from "./context";
import { getLocaleFromCookie, setLocaleInCookie } from "./cookies";
2026-01-20 14:06:47 +08:00
import { enUS } from "./locales/en-US";
import { zhCN } from "./locales/zh-CN";
import { detectLocale, type Locale, type Translations } from "./index";
const translations: Record<Locale, Translations> = {
"en-US": enUS,
"zh-CN": zhCN,
};
export function useI18n() {
2026-01-20 16:00:39 +08:00
const { locale, setLocale } = useI18nContext();
2026-01-20 14:06:47 +08:00
const t = translations[locale];
const changeLocale = (newLocale: Locale) => {
setLocale(newLocale);
2026-01-20 16:00:39 +08:00
setLocaleInCookie(newLocale);
2026-01-20 14:06:47 +08:00
};
// Initialize locale on mount
useEffect(() => {
2026-01-20 16:00:39 +08:00
const saved = getLocaleFromCookie() as Locale | null;
if (!saved) {
const detected = detectLocale();
setLocale(detected);
setLocaleInCookie(detected);
2026-01-20 14:06:47 +08:00
}
2026-01-20 16:00:39 +08:00
}, [setLocale]);
2026-01-20 14:06:47 +08:00
return {
locale,
t,
changeLocale,
};
}