Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supports selecting the overwrite order of merge messages #1896

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/shared/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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] })
Expand Down
5 changes: 3 additions & 2 deletions packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,8 @@ export function createComposer(options: any = {}): any {
// mergeLocaleMessage
function mergeLocaleMessage(
locale: Locale,
message: LocaleMessageDictionary<Message>
message: LocaleMessageDictionary<Message>,
isRetainExistMessage: boolean = false
): void {
_messages.value[locale] = _messages.value[locale] || {}
const _message = { [locale]: message }
Expand All @@ -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
}

Expand Down