From dad192b344ce04988ffdc9b431aa2ca67603cc02 Mon Sep 17 00:00:00 2001 From: supercpq Date: Wed, 17 Jul 2024 22:30:37 +0800 Subject: [PATCH 1/2] feat: supports selecting the overwrite order of merge messages --- packages/shared/src/messages.ts | 6 ++++-- packages/vue-i18n-core/src/composer.ts | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/messages.ts b/packages/shared/src/messages.ts index d657eed02..ddc89afa2 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,9 @@ 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 + !isRetainExistMessage && (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 } From 67382d020770f366b6d7a4e6235cbed5ac00eba8 Mon Sep 17 00:00:00 2001 From: supercpq <87625173+supercpq@users.noreply.github.com> Date: Wed, 17 Jul 2024 23:56:25 +0800 Subject: [PATCH 2/2] feat: supports selecting the overwrite order of merge messages --- packages/shared/src/messages.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/shared/src/messages.ts b/packages/shared/src/messages.ts index ddc89afa2..e7bbcf9bf 100644 --- a/packages/shared/src/messages.ts +++ b/packages/shared/src/messages.ts @@ -19,7 +19,9 @@ export function deepCopy(src: any, des: any, isRetainExistMessage?: boolean): vo // src[key] or des[key] is an array // if you need to keep the existing message on the i18n instance when merging, // you do not need to perform the following replacement - !isRetainExistMessage && (des[key] = src[key]) + 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] })