import { createI18n } from 'vue-i18n' import { getStorage, getBrowserLang } from '@/utils/index' import enLocale from './modules/en' import cnLocale from './modules/CN' import zhcnLocale from './modules/zh-CN' import korcnLocale from './modules/Korean' import japcnLocale from './modules/Japanese' import Deutsch from './modules/de' import fr from './modules/fr' import vi from './modules/vi' import th from './modules/th' import gr from './modules/gr' import Italy from './modules/Italy' import SpanishLocal from './modules/es' import PortugueseLocal from './modules/pt' // 支持的 locale 列表,用于校验 const SUPPORTED_LOCALES = ['en', 'CN', 'zh-CN', 'Korean', 'Japanese', 'de', 'fr', 'vi', 'th', 'Italy', 'es', 'pt', 'gr'] // 获取初始语言:缓存 > 浏览器语言,并确保在支持列表中 function getInitialLocale() { const cached = getStorage('lang') if (cached && SUPPORTED_LOCALES.includes(cached)) return cached const browserLang = getBrowserLang() return SUPPORTED_LOCALES.includes(browserLang) ? browserLang : 'en' } const messages = { en: { ...enLocale }, CN: { ...cnLocale }, 'zh-CN': { ...zhcnLocale }, Korean: { ...korcnLocale }, Japanese: { ...japcnLocale }, de: { ...Deutsch }, fr: { ...fr }, vi: { ...vi }, th: { ...th }, Italy: { ...Italy }, es: { ...SpanishLocal }, pt: { ...PortugueseLocal }, gr: { ...gr } } const i18n = createI18n({ legacy: false, locale: getInitialLocale(), fallbackLocale: ['zh-CN', 'en'], // 缺失 key 时先回退到简体中文,再回退到英文 messages, missingWarn: import.meta.env.DEV, // 仅开发环境输出缺失 key 警告 fallbackWarn: import.meta.env.DEV, silentTranslationWarn: !import.meta.env.DEV }) export default i18n