diff --git a/packages/shared/src/messages.ts b/packages/shared/src/messages.ts index d657eed02..e7bbcf9bf 100644 --- a/packages/shared/src/messages.ts +++ b/packages/shared/src/messages.ts @@ -2,7 +2,7 @@ import { isArray, isObject } from './utils' const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val) // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function deepCopy(src: any, des: any): void { +export function deepCopy(src: any, des: any, isRetainExistMessage?: boolean): void { // src and des should both be objects, and none of them can be a array if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) { throw new Error('Invalid value') @@ -17,7 +17,11 @@ export function deepCopy(src: any, des: any): void { // replace with src[key] when: // src[key] or des[key] is not an object, or // src[key] or des[key] is an array - des[key] = src[key] + // if you need to keep the existing message on the i18n instance when merging, + // you do not need to perform the following replacement + if (!isRetainExistMessage || typeof des[key] === 'undefined') { + des[key] = src[key] + } } else { // src[key] and des[key] are both objects, merge them stack.push({ src: src[key], des: des[key] }) diff --git a/packages/vue-i18n-core/src/composer.ts b/packages/vue-i18n-core/src/composer.ts index b5bc124e3..4c003090d 100644 --- a/packages/vue-i18n-core/src/composer.ts +++ b/packages/vue-i18n-core/src/composer.ts @@ -2458,7 +2458,8 @@ export function createComposer(options: any = {}): any { // mergeLocaleMessage function mergeLocaleMessage( locale: Locale, - message: LocaleMessageDictionary + message: LocaleMessageDictionary, + isRetainExistMessage: boolean = false ): void { _messages.value[locale] = _messages.value[locale] || {} const _message = { [locale]: message } @@ -2470,7 +2471,7 @@ export function createComposer(options: any = {}): any { } } message = _message[locale] - deepCopy(message, _messages.value[locale]) + deepCopy(message, _messages.value[locale], isRetainExistMessage) _context.messages = _messages.value as typeof _context.messages }