diff --git a/package.json b/package.json index 971de77..cf96400 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "shrimp-admin", - "version": "0.2.0", + "version": "0.2.1", "private": true, "description": "Shrimp 框架前端 — 消费 ShrimpServer + ShrimpTable 契约的经典 Vue3 + Element Plus 实现", "main": "src/index.ts", diff --git a/src/VueAdmin.ts b/src/VueAdmin.ts index dd36041..df64bb7 100644 --- a/src/VueAdmin.ts +++ b/src/VueAdmin.ts @@ -12,6 +12,22 @@ import { IShrimpResponse, ILabel } from './IShrimpType' import { ShrimpClient, handleShrimpResponse, type IShrimpTipper, type IRouteInfo } from './ShrimpClient' import FieldString from './components/fields/FieldString.vue' +// ============== 工具 ============== + +/** locale 深合并:嵌套对象按 key 递归 merge,非对象值 src 覆盖 dst。仅服务于 i18n messages,没必要拉 lodash。 */ +function deepMerge(dst: Record, src: Record): Record { + const out: Record = { ...dst } + for (const key in src) { + const sv = src[key], dv = out[key] + if (sv && typeof sv === 'object' && !Array.isArray(sv) && dv && typeof dv === 'object' && !Array.isArray(dv)) { + out[key] = deepMerge(dv, sv) + } else { + out[key] = sv + } + } + return out +} + // ============== Options ============== export interface ShrimpAdminConfig { @@ -169,14 +185,14 @@ export class VueAdmin extends ShrimpClient { } initI18n(extraLocales?: Record) { - const merged: Record = { - 'zh-cn': { ...zh_cn, ...(extraLocales?.['zh-cn'] || {}) }, - 'en': { ...en, ...(extraLocales?.['en'] || {}) }, - } - if (extraLocales) { - for (const key in extraLocales) { - if (!(key in merged)) merged[key] = extraLocales[key] - } + // 深合并:框架自带 locale 在底,业务侧叠在上。 + // 旧版用 spread 浅合并,业务侧只要定义了同名一级键(如 user)就会把框架的 user.* 全部替换, + // 表现是控制台 [intlify] Not found 'user.operationLog' key 之类的报错。 + const builtins: Record = { 'zh-cn': zh_cn, 'en': en } + const merged: Record = {} + const langs = new Set([...Object.keys(builtins), ...Object.keys(extraLocales || {})]) + for (const lang of langs) { + merged[lang] = deepMerge(builtins[lang] || {}, extraLocales?.[lang] || {}) } this.i18n = createI18n({ locale: this.localData.get("lang") || 'zh-cn',