import { createI18n } from 'vue-i18n' import EN from './en.json' import CN from './zh-cn.json' const messages = { en: { ...EN }, 'zh-cn': { ...CN } } // 默认语言从环境变量获取 const DEFAULT_LANG = import.meta.env.VITE_APP_LANG || 'zh-cn' // 开发环境下重置语言为环境变量值(方便调试) if (import.meta.env.DEV) { localStorage.setItem('language', JSON.stringify(DEFAULT_LANG)) } const getCurrentLanguage = () => { // 从 localStorage 读取语言设置,与 app store 保持一致 // useStorage 使用 JSON 序列化,所以需要 parse try { const stored = localStorage.getItem('language') if (stored) { const language = JSON.parse(stored) if (language === 'zh-cn' || language === 'en') { return language } } } catch { // ignore parse error } return DEFAULT_LANG } const i18n = createI18n({ legacy: false, globalInjection: true, locale: getCurrentLanguage(), messages, fallbackLocale: DEFAULT_LANG }) export default i18n