front/src/locale/index.ts
zzh d3e1c62043 perf: 优化登录进系统链路,减轻首屏与进系统阻塞
登录页延后重型依赖与角色动画,i18n 拆登录轻量包;进系统只等待动态路由与完整语言包,EP/VXE 后台预热。兼容登录响应带回 userInfo。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 23:13:23 +08:00

77 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string>(['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<void> {
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<void> {
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<void> {
if (defaultLocale !== 'en-US') return;
await loadBootstrapEn();
i18n.global.locale.value = 'en-US';
}
export default i18n;