mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-06-13 19:50:36 +08:00
添加国际化支持:引入 vue-i18n,更新多个组件以支持语言切换和文本翻译
This commit is contained in:
69
src/plugins/i18n.ts
Normal file
69
src/plugins/i18n.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { createI18n } from 'vue-i18n'
|
||||
import { nextTick } from 'vue'
|
||||
import { SUPPORTED_LOCALES, SupportedLocale } from '@/locales/types'
|
||||
|
||||
// 导入语言文件
|
||||
import zhCN from '@/locales/zh-CN'
|
||||
import enUS from '@/locales/en-US'
|
||||
|
||||
// 创建 i18n 实例
|
||||
const i18n = createI18n({
|
||||
legacy: false, // 使用组合式API
|
||||
locale: getBrowserLocale() || 'zh-CN', // 默认语言
|
||||
fallbackLocale: 'zh-CN', // 回退语言
|
||||
messages: {
|
||||
'zh-CN': zhCN,
|
||||
'en-US': enUS,
|
||||
},
|
||||
silentTranslationWarn: true,
|
||||
silentFallbackWarn: true,
|
||||
})
|
||||
|
||||
/**
|
||||
* 获取浏览器语言设置
|
||||
*/
|
||||
export function getBrowserLocale(): SupportedLocale | null {
|
||||
// 从本地存储获取
|
||||
const storedLocale = localStorage.getItem('MP_LOCALE')
|
||||
if (storedLocale && Object.keys(SUPPORTED_LOCALES).includes(storedLocale)) {
|
||||
return storedLocale as SupportedLocale
|
||||
}
|
||||
|
||||
// 从浏览器获取
|
||||
const navigatorLocale = navigator.languages?.[0] || navigator.language || 'zh-CN'
|
||||
|
||||
// 检查是否为支持的语言
|
||||
const locale = Object.keys(SUPPORTED_LOCALES).find(locale => {
|
||||
return navigatorLocale.includes(locale.split('-')[0])
|
||||
})
|
||||
|
||||
return (locale as SupportedLocale) || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置i18n语言环境
|
||||
*/
|
||||
export async function setI18nLanguage(locale: SupportedLocale) {
|
||||
// 加载语言文件(如果使用动态导入)
|
||||
// await loadLocaleMessages(i18n, locale)
|
||||
|
||||
// 更新 i18n 实例语言
|
||||
i18n.global.locale.value = locale as any as any
|
||||
|
||||
// 保存到本地存储
|
||||
localStorage.setItem('MP_LOCALE', locale)
|
||||
|
||||
// 更新 HTML 标签 lang 属性
|
||||
document.querySelector('html')?.setAttribute('lang', locale)
|
||||
|
||||
return nextTick()
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前语言
|
||||
*/
|
||||
export function getCurrentLocale(): SupportedLocale {
|
||||
return i18n.global.locale.value as SupportedLocale
|
||||
}
|
||||
|
||||
export default i18n
|
||||
Reference in New Issue
Block a user