mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-27 07:44:48 +08:00
feat: implement i18n
This commit is contained in:
57
frontend/src/core/i18n/hooks.ts
Normal file
57
frontend/src/core/i18n/hooks.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
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() {
|
||||
const [locale, setLocale] = useState<Locale>(() => {
|
||||
if (typeof window === "undefined") {
|
||||
return "en-US";
|
||||
}
|
||||
|
||||
// Try to get from localStorage first
|
||||
const saved = localStorage.getItem("locale") as Locale | null;
|
||||
if (saved && (saved === "en-US" || saved === "zh-CN")) {
|
||||
return saved;
|
||||
}
|
||||
|
||||
// Otherwise detect from browser
|
||||
return detectLocale();
|
||||
});
|
||||
|
||||
const t = translations[locale];
|
||||
|
||||
const changeLocale = (newLocale: Locale) => {
|
||||
setLocale(newLocale);
|
||||
if (typeof window !== "undefined") {
|
||||
localStorage.setItem("locale", newLocale);
|
||||
}
|
||||
};
|
||||
|
||||
// Initialize locale on mount
|
||||
useEffect(() => {
|
||||
if (typeof window !== "undefined") {
|
||||
const saved = localStorage.getItem("locale") as Locale | null;
|
||||
if (!saved) {
|
||||
const detected = detectLocale();
|
||||
setLocale(detected);
|
||||
localStorage.setItem("locale", detected);
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
return {
|
||||
locale,
|
||||
t,
|
||||
changeLocale,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user