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