设置页面修改语言和Agent,其它页面实时更新状态

This commit is contained in:
xintaofei
2026-03-07 21:36:00 +08:00
parent 8f265f8c0c
commit 62d8e1a0b0
5 changed files with 215 additions and 12 deletions

View File

@@ -34,6 +34,7 @@ interface AppI18nContextValue {
}
const AppI18nContext = createContext<AppI18nContextValue | null>(null)
const LANGUAGE_SETTINGS_UPDATED_EVENT = "app://language-settings-updated"
function subscribeSystemLocale(onStoreChange: () => void) {
if (typeof window === "undefined") return () => {}
@@ -128,6 +129,57 @@ export function AppI18nProvider({
[]
)
useEffect(() => {
if (typeof window === "undefined") return
const onStorage = (event: StorageEvent) => {
if (event.key !== LANGUAGE_SETTINGS_STORAGE_KEY || !event.newValue) return
try {
const next = normalizeLanguageSettings(
JSON.parse(event.newValue) as SystemLanguageSettings
)
setLanguageSettingsState(next)
} catch {
// Ignore malformed storage payloads.
}
}
window.addEventListener("storage", onStorage)
let unlisten: (() => void) | null = null
let cancelled = false
void import("@tauri-apps/api/event")
.then(({ listen }) =>
listen<SystemLanguageSettings>(
LANGUAGE_SETTINGS_UPDATED_EVENT,
(event) => {
if (cancelled) return
setLanguageSettings(event.payload)
}
)
)
.then((dispose) => {
if (cancelled) {
dispose()
return
}
unlisten = dispose
})
.catch(() => {
// Ignore when running in non-tauri environment.
})
return () => {
cancelled = true
window.removeEventListener("storage", onStorage)
if (unlisten) {
unlisten()
}
}
}, [setLanguageSettings])
useEffect(() => {
let cancelled = false