refactor: remove LocaleSwitcher component and update locale handling

- 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.
This commit is contained in:
mlogclub
2026-06-26 14:46:01 +08:00
parent 12d188fc7e
commit f827a7471c
32 changed files with 215 additions and 268 deletions
+7 -22
View File
@@ -25,7 +25,7 @@ async function loadConfig() {
test("normalizes supported locale aliases", async () => {
const { DEFAULT_LOCALE, normalizeLocale } = await loadConfig()
assert.equal(DEFAULT_LOCALE, "en-US")
assert.equal(DEFAULT_LOCALE, "zh-CN")
assert.equal(normalizeLocale("zh-CN"), "zh-CN")
assert.equal(normalizeLocale("zh_CN"), "zh-CN")
assert.equal(normalizeLocale("zh"), "zh-CN")
@@ -35,26 +35,11 @@ test("normalizes supported locale aliases", async () => {
assert.equal(normalizeLocale("fr-FR"), DEFAULT_LOCALE)
})
test("resolves browser locale from stored value before navigator languages", async () => {
const { resolveBrowserLocale } = await loadConfig()
test("reads the configured locale without browser language detection", async () => {
const { configureLocale, readStoredLocale } = await loadConfig()
assert.equal(
resolveBrowserLocale({
storedLocale: "en-US",
navigatorLanguages: ["zh-CN"],
}),
"en-US"
)
})
test("falls back through navigator languages", async () => {
const { resolveBrowserLocale } = await loadConfig()
assert.equal(
resolveBrowserLocale({
storedLocale: "",
navigatorLanguages: ["fr-FR", "en"],
}),
"en-US"
)
assert.equal(readStoredLocale(), "zh-CN")
configureLocale("en-US")
assert.equal(readStoredLocale(), "en-US")
})
+6 -34
View File
@@ -1,8 +1,7 @@
export const SUPPORTED_LOCALES = ["zh-CN", "en-US"] as const
export type AppLocale = (typeof SUPPORTED_LOCALES)[number]
export const DEFAULT_LOCALE: AppLocale = "en-US"
export const LOCALE_STORAGE_KEY = "cs_ai_agent_locale"
export const DEFAULT_LOCALE: AppLocale = "zh-CN"
const LOCALE_ALIASES: Record<string, AppLocale> = {
zh: "zh-CN",
@@ -37,40 +36,13 @@ export function isSupportedLocale(
return SUPPORTED_LOCALES.includes(value as AppLocale)
}
export function resolveBrowserLocale({
storedLocale,
navigatorLanguages,
}: {
storedLocale?: string | null
navigatorLanguages?: readonly string[] | null
}): AppLocale {
if (isSupportedLocale(storedLocale)) {
return storedLocale
}
for (const locale of navigatorLanguages ?? []) {
const normalized = normalizeSupportedLocale(locale)
if (normalized) {
return normalized
}
}
return DEFAULT_LOCALE
}
let configuredLocale: AppLocale = DEFAULT_LOCALE
export function readStoredLocale(): AppLocale {
if (typeof window === "undefined") {
return DEFAULT_LOCALE
}
return resolveBrowserLocale({
storedLocale: window.localStorage.getItem(LOCALE_STORAGE_KEY),
navigatorLanguages: window.navigator.languages,
})
return configuredLocale
}
export function writeStoredLocale(locale: AppLocale) {
if (typeof window === "undefined") {
return
}
window.localStorage.setItem(LOCALE_STORAGE_KEY, locale)
export function configureLocale(locale: string | null | undefined): AppLocale {
configuredLocale = normalizeLocale(locale)
return configuredLocale
}
+21 -13
View File
@@ -12,10 +12,10 @@ import {
import {
DEFAULT_LOCALE,
type AppLocale,
readStoredLocale,
writeStoredLocale,
configureLocale,
} from "@/i18n/config"
import { translateMessage } from "@/i18n/messages"
import { fetchPublicConfig } from "@/lib/api/config"
type LocaleContextValue = {
locale: AppLocale
@@ -34,11 +34,24 @@ export function AppI18nProvider({ children }: { children: ReactNode }) {
const [isLocaleReady, setIsLocaleReady] = useState(false)
useEffect(() => {
const storedLocale = readStoredLocale()
setLocaleState(storedLocale)
document.documentElement.lang = storedLocale
document.title = translateMessage(storedLocale, "app.metadataTitle")
setIsLocaleReady(true)
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(() => {
@@ -49,12 +62,7 @@ export function AppI18nProvider({ children }: { children: ReactNode }) {
() => ({
locale,
t: (key, values) => translateMessage(locale, key, values),
setLocale: (nextLocale) => {
setLocaleState(nextLocale)
writeStoredLocale(nextLocale)
document.documentElement.lang = nextLocale
document.title = translateMessage(nextLocale, "app.metadataTitle")
},
setLocale: () => {},
}),
[locale]
)