feat: 实现i18n核心配置和语言状态管理

- 创建i18n配置系统,支持简体中文/繁体中文/英文三种语言
- 实现浏览器语言自动检测和localStorage持久化
- 添加基础翻译文件,包含common、language、header、apiStats模块
- 创建locale store使用Pinia管理语言状态
- 配置语言标识符为纯文字:简/繁/EN,去除国旗emoji
This commit is contained in:
Wangnov
2025-09-08 15:40:48 +08:00
parent 504b9e3ea7
commit cb1b7bc0e3
5 changed files with 224 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import { createI18n } from 'vue-i18n'
import zhCn from './locales/zh-cn.js'
import zhTw from './locales/zh-tw.js'
import en from './locales/en.js'
// 获取浏览器语言设置
function getBrowserLocale() {
const navigatorLocale = navigator.languages ? navigator.languages[0] : navigator.language
if (!navigatorLocale) {
return 'zh-cn'
}
const trimmedLocale = navigatorLocale.trim().split(';')[0].toLowerCase()
if (trimmedLocale.includes('zh')) {
if (
trimmedLocale.includes('tw') ||
trimmedLocale.includes('hk') ||
trimmedLocale.includes('mo')
) {
return 'zh-tw'
}
return 'zh-cn'
}
if (trimmedLocale.includes('en')) {
return 'en'
}
return 'zh-cn' // 默认简体中文
}
// 获取保存的语言设置或浏览器语言
const savedLocale = localStorage.getItem('app-locale')
const defaultLocale = savedLocale || getBrowserLocale()
export const SUPPORTED_LOCALES = {
'zh-cn': {
name: '简体中文',
flag: '简',
shortName: '简'
},
'zh-tw': {
name: '繁體中文',
flag: '繁',
shortName: '繁'
},
en: {
name: 'English',
flag: 'EN',
shortName: 'EN'
}
}
export const i18n = createI18n({
legacy: false, // 使用 Composition API 模式
locale: defaultLocale,
fallbackLocale: 'zh-cn',
messages: {
'zh-cn': zhCn,
'zh-tw': zhTw,
en: en
},
globalInjection: true // 全局注入 $t 函数
})
export default i18n