This commit is contained in:
parent
81e668967d
commit
4696d3f364
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue