front/src/locale/index.ts
zzh 631d113eea
All checks were successful
Build and Deploy Front / deploy (push) Successful in 5m39s
feat: 运单管理支持获取下游文件
工具栏增加获取下游面单/末端面单/POD,并修复语言包热更新后按钮显示 key。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-10 18:43:29 +08:00

91 lines
2.6 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';
}
/** 开发态:语言包热更新后立即灌入 i18n,避免按钮显示 key */
if (import.meta.hot) {
import.meta.hot.accept('./zh-CN', (mod) => {
if (!mod?.default) return;
i18n.global.setLocaleMessage('zh-CN', mod.default);
fullLocaleLoaded.add('zh-CN-full');
});
import.meta.hot.accept('./en-US', (mod) => {
if (!mod?.default) return;
i18n.global.setLocaleMessage('en-US', mod.default);
fullLocaleLoaded.add('en-US-full');
});
}
export default i18n;