This commit is contained in:
hailin 2025-04-17 11:22:06 +08:00
parent 81e668967d
commit 4696d3f364
1 changed files with 4 additions and 5 deletions

View File

@ -13,7 +13,6 @@ export function LanguageSwitcher() {
const currentLocale = pathname.split('/')[1] || i18nConfig.defaultLocale
useEffect(() => {
// 第一次访问页面时,如果 URL 有语言,就记住
if (typeof window !== 'undefined') {
const saved = localStorage.getItem('preferred-language')
if (!saved || saved !== currentLocale) {
@ -29,15 +28,15 @@ export function LanguageSwitcher() {
localStorage.setItem('preferred-language', newLocale)
}
// 拆解当前路径
const segments = pathname.split('/')
// 如果路径前缀是语言码,则替换
const isLocaleInPath = (i18nConfig.locales as unknown as string[]).includes(segments[1])
// ✅ 修复:类型断言 readonly -> string[]
const localeList = [...i18nConfig.locales] as string[]
const isLocaleInPath = localeList.includes(segments[1])
if (isLocaleInPath) {
segments[1] = newLocale
} else {
// 否则插入语言前缀
segments.unshift(newLocale)
}