f827a7471c
- Removed LocaleSwitcher from NavUser, SiteHeader, and WorkbenchHeader components. - Updated tests to reflect the removal of LocaleSwitcher. - Changed default locale from "en-US" to "zh-CN" in i18n configuration. - Refactored locale resolution logic to read configured locale without relying on browser language detection. - Updated AgentDesk SDK to support language configuration from public API. - Cleaned up unused auth options fetching in the auth API.
88 lines
1.9 KiB
TypeScript
88 lines
1.9 KiB
TypeScript
"use client"
|
|
|
|
import {
|
|
createContext,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
type ReactNode,
|
|
} from "react"
|
|
|
|
import {
|
|
DEFAULT_LOCALE,
|
|
type AppLocale,
|
|
configureLocale,
|
|
} from "@/i18n/config"
|
|
import { translateMessage } from "@/i18n/messages"
|
|
import { fetchPublicConfig } from "@/lib/api/config"
|
|
|
|
type LocaleContextValue = {
|
|
locale: AppLocale
|
|
setLocale: (locale: AppLocale) => void
|
|
t: (key: string, values?: Record<string, string | number>) => string
|
|
}
|
|
|
|
const LocaleContext = createContext<LocaleContextValue>({
|
|
locale: DEFAULT_LOCALE,
|
|
setLocale: () => {},
|
|
t: (key) => key,
|
|
})
|
|
|
|
export function AppI18nProvider({ children }: { children: ReactNode }) {
|
|
const [locale, setLocaleState] = useState<AppLocale>(DEFAULT_LOCALE)
|
|
const [isLocaleReady, setIsLocaleReady] = useState(false)
|
|
|
|
useEffect(() => {
|
|
let cancelled = false
|
|
|
|
fetchPublicConfig()
|
|
.then((config) => configureLocale(config.language))
|
|
.catch(() => configureLocale(DEFAULT_LOCALE))
|
|
.then((configuredLocale) => {
|
|
if (cancelled) {
|
|
return
|
|
}
|
|
setLocaleState(configuredLocale)
|
|
document.documentElement.lang = configuredLocale
|
|
document.title = translateMessage(configuredLocale, "app.metadataTitle")
|
|
setIsLocaleReady(true)
|
|
})
|
|
|
|
return () => {
|
|
cancelled = true
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
document.title = translateMessage(locale, "app.metadataTitle")
|
|
}, [locale])
|
|
|
|
const value = useMemo<LocaleContextValue>(
|
|
() => ({
|
|
locale,
|
|
t: (key, values) => translateMessage(locale, key, values),
|
|
setLocale: () => {},
|
|
}),
|
|
[locale]
|
|
)
|
|
|
|
if (!isLocaleReady) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<LocaleContext.Provider value={value}>
|
|
{children}
|
|
</LocaleContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useAppLocale() {
|
|
return useContext(LocaleContext)
|
|
}
|
|
|
|
export function useI18n() {
|
|
return useContext(LocaleContext).t
|
|
}
|