This commit is contained in:
hailin 2025-04-17 11:07:55 +08:00
parent 684764a90f
commit 85cf4c1a92
1 changed files with 12 additions and 4 deletions

View File

@ -25,19 +25,27 @@ export function LanguageSwitcher() {
const handleChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
const newLocale = e.target.value
// 保存用户选择
if (typeof window !== 'undefined') {
localStorage.setItem('preferred-language', newLocale)
}
// 替换语言部分的路径
// 拆解当前路径
const segments = pathname.split('/')
segments[1] = newLocale
// 如果路径前缀是语言码,则替换
const isLocaleInPath = i18nConfig.locales.includes(segments[1])
if (isLocaleInPath) {
segments[1] = newLocale
} else {
// 否则插入语言前缀
segments.unshift(newLocale)
}
const newPath = segments.join('/')
startTransition(() => {
router.push(newPath)
router.refresh()
router.refresh()
})
}