diff --git a/src/editor/core/i18n/I18n.ts b/src/editor/core/i18n/I18n.ts index 4b2e0f3..aaafc08 100644 --- a/src/editor/core/i18n/I18n.ts +++ b/src/editor/core/i18n/I18n.ts @@ -1,6 +1,8 @@ import { ILang } from '../../interface/i18n/I18n' import zhCN from './lang/zh-CN.json' import en from './lang/en.json' +import { mergeObject } from '../../utils' +import { DeepPartial } from '../../interface/Common' export class I18n { @@ -11,8 +13,12 @@ export class I18n { private currentLocale = 'zhCN' - public registerLangMap(locale: string, lang: ILang) { - this.langMap.set(locale, lang) + public registerLangMap(locale: string, lang: DeepPartial) { + const sourceLang = this.langMap.get(locale) + this.langMap.set( + locale, + mergeObject(sourceLang || zhCN, lang) + ) } public setLocale(locale: string) { diff --git a/src/editor/core/register/Register.ts b/src/editor/core/register/Register.ts index 6a24f55..d7ac853 100644 --- a/src/editor/core/register/Register.ts +++ b/src/editor/core/register/Register.ts @@ -4,6 +4,7 @@ import { ContextMenu } from '../contextmenu/ContextMenu' import { Shortcut } from '../shortcut/Shortcut' import { I18n } from '../i18n/I18n' import { ILang } from '../../interface/i18n/I18n' +import { DeepPartial } from '../../interface/Common' interface IRegisterPayload { contextMenu: ContextMenu; @@ -15,7 +16,7 @@ export class Register { public contextMenuList: (payload: IRegisterContextMenu[]) => void public shortcutList: (payload: IRegisterShortcut[]) => void - public langMap: (locale: string, lang: ILang) => void + public langMap: (locale: string, lang: DeepPartial) => void constructor(payload: IRegisterPayload) { const { contextMenu, shortcut, i18n } = payload diff --git a/src/editor/interface/Common.ts b/src/editor/interface/Common.ts index 45c1e75..13fa102 100644 --- a/src/editor/interface/Common.ts +++ b/src/editor/interface/Common.ts @@ -22,4 +22,8 @@ export type DeepRequired = T extends Error ? Promise> : T extends {} ? { [K in keyof T]-?: DeepRequired } - : Required \ No newline at end of file + : Required + +export type DeepPartial = { + [P in keyof T]?: DeepPartial; +} \ No newline at end of file diff --git a/src/editor/utils/index.ts b/src/editor/utils/index.ts index a93677c..a6383ab 100644 --- a/src/editor/utils/index.ts +++ b/src/editor/utils/index.ts @@ -87,4 +87,28 @@ function nClickEvent(n: number, dom: HTMLElement, fn: (evt: MouseEvent) => any) } dom.addEventListener('click', handler) +} + +export function isObject(type: unknown): type is Record { + return Object.prototype.toString.call(type) === '[object Object]' +} + +export function isArray(type: unknown): type is Array { + return Array.isArray(type) +} + +export function mergeObject(source: T, target: T): T { + if (isObject(source) && isObject(target)) { + const objectTarget = >target + for (const [key, val] of Object.entries(source)) { + if (!objectTarget[key]) { + objectTarget[key] = val + } else { + objectTarget[key] = mergeObject(val, objectTarget[key]) + } + } + } else if (isArray(source) && isArray(target)) { + target.push(...source) + } + return target } \ No newline at end of file