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,42 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { i18n, SUPPORTED_LOCALES } from '@/i18n'
export const useLocaleStore = defineStore('locale', () => {
const currentLocale = ref(i18n.global.locale.value)
// 切换语言
const setLocale = (locale) => {
if (!SUPPORTED_LOCALES[locale]) {
console.warn(`Unsupported locale: ${locale}`)
return
}
currentLocale.value = locale
i18n.global.locale.value = locale
localStorage.setItem('app-locale', locale)
// 更新HTML lang属性
document.documentElement.setAttribute('lang', locale)
}
// 获取当前语言信息
const getCurrentLocaleInfo = () => {
return SUPPORTED_LOCALES[currentLocale.value] || SUPPORTED_LOCALES['zh-cn']
}
// 获取所有支持的语言
const getSupportedLocales = () => {
return Object.entries(SUPPORTED_LOCALES).map(([key, value]) => ({
code: key,
...value
}))
}
return {
currentLocale,
setLocale,
getCurrentLocaleInfo,
getSupportedLocales
}
})