import { createI18n } from 'vue-i18n'; import bootstrapZh from './bootstrap-zh-CN'; export const LOCALE_OPTIONS = [ { label: '中文', value: 'zh-CN' }, { label: 'English', value: 'en-US' }, ]; const defaultLocale = localStorage.getItem('arco-locale') || 'zh-CN'; const fullLocaleLoaded = new Set(['zh-CN-bootstrap']); const i18n = createI18n({ locale: defaultLocale === 'en-US' ? 'zh-CN' : defaultLocale, fallbackLocale: 'zh-CN', legacy: false, allowComposition: true, messages: { 'zh-CN': bootstrapZh, }, silentTranslationWarn: true, missingWarn: false, fallbackWarn: false, }); async function loadBootstrapEn() { if (fullLocaleLoaded.has('en-US-bootstrap')) return; const mod = await import('./bootstrap-en-US'); i18n.global.setLocaleMessage('en-US', mod.default); fullLocaleLoaded.add('en-US-bootstrap'); } /** 按需加载完整业务语言包(进系统前调用) */ export async function ensureFullLocaleMessages( locale: string = i18n.global.locale.value, ): Promise { const key = `${locale}-full`; if (fullLocaleLoaded.has(key)) return; if (locale === 'zh-CN') { const mod = await import('./zh-CN'); i18n.global.setLocaleMessage('zh-CN', mod.default); fullLocaleLoaded.add(key); return; } if (locale === 'en-US') { await loadBootstrapEn(); const mod = await import('./en-US'); i18n.global.setLocaleMessage('en-US', mod.default); fullLocaleLoaded.add(key); } } /** 登录页切换语言:优先轻量包,后台补全量 */ export async function ensureLocaleMessages(locale: string): Promise { if (locale === 'zh-CN') { // 已有 bootstrap;若已进过系统则已是 full return; } if (locale === 'en-US') { if (fullLocaleLoaded.has('en-US-full')) return; await loadBootstrapEn(); // 后台拉全量,不堵切换 void ensureFullLocaleMessages('en-US'); } } /** 启动时若偏好英文,先加载英文轻量包再切语言 */ export async function initPreferredLocale(): Promise { if (defaultLocale !== 'en-US') return; await loadBootstrapEn(); i18n.global.locale.value = 'en-US'; } export default i18n;