feat(i18n): 切换语言时同步更新页面标题

- resolveDocumentTitle() 新增 titleKey 参数,优先通过 i18n 翻译
- router beforeEach 中将路由 meta.titleKey 传入标题解析函数
- setLocale() 切换语言后同步刷新 document.title
This commit is contained in:
wucm667
2026-02-26 14:04:13 +08:00
parent c75c6b6858
commit 82fbf452a8
3 changed files with 26 additions and 5 deletions

View File

@@ -68,6 +68,14 @@ export async function setLocale(locale: string): Promise<void> {
i18n.global.locale.value = locale
localStorage.setItem(LOCALE_KEY, locale)
document.documentElement.setAttribute('lang', locale)
// 同步更新浏览器页签标题,使其跟随语言切换
const { resolveDocumentTitle } = await import('@/router/title')
const { default: router } = await import('@/router')
const { useAppStore } = await import('@/stores/app')
const route = router.currentRoute.value
const appStore = useAppStore()
document.title = resolveDocumentTitle(route.meta.title, appStore.siteName, route.meta.titleKey as string)
}
export function getLocale(): LocaleCode {