Skip to content
Merged
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
28 changes: 25 additions & 3 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ import {
isRegExp,
isBoolean,
isPlainObject,
makeSymbol
makeSymbol,
isObject
} from './utils'

// extend VNode interface
Expand Down Expand Up @@ -269,7 +270,7 @@ function getLocaleMessages<
const { messages, __i18n } = options

// prettier-ignore
let ret = isPlainObject(messages)
const ret = isPlainObject(messages)
? messages
: isArray(__i18n)
? {}
Expand All @@ -278,7 +279,7 @@ function getLocaleMessages<
// merge locale messages of i18n custom block
if (isArray(__i18n)) {
__i18n.forEach(raw => {
ret = Object.assign(ret, isString(raw) ? JSON.parse(raw) : raw)
deepCopy(isString(raw) ? JSON.parse(raw) : raw, ret)
})
return ret
}
Expand All @@ -291,6 +292,27 @@ function getLocaleMessages<
return ret
}

const hasOwnProperty = Object.prototype.hasOwnProperty
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function hasOwn(obj: object | Array<any>, key: string): boolean {
return hasOwnProperty.call(obj, key)
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deepCopy(source: any, destination: any): void {
for (const key in source) {
if (hasOwn(source, key)) {
if (!isObject(source[key])) {
destination[key] = destination[key] != null ? destination[key] : {}
destination[key] = source[key]
} else {
destination[key] = destination[key] != null ? destination[key] : {}
deepCopy(source[key], destination[key])
}
}
}
}

export function addPreCompileMessages<Message = VueMessageType>(
messages: LocaleMessages<Message>,
functions: MessageFunctions<Message>
Expand Down
55 changes: 55 additions & 0 deletions test/composer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,61 @@ describe('__i18n', () => {
}
})
})

test('merge locale messages', () => {
const msgFn = () => 'ふー'
const options = {
__i18n: [
JSON.stringify({ en: { hello: 'Hello,world!' } }),
JSON.stringify({ ja: { hello: 'こんにちは、世界!' } })
],
messages: {
en: { foo: 'foo' },
ja: { foo: msgFn }
}
}
const { messages } = createComposer(
options as ComposerOptions<VueMessageType>
)
expect(messages.value.en).toEqual({
hello: 'Hello,world!',
foo: 'foo'
})
expect(messages.value.ja).toEqual({
hello: 'こんにちは、世界!',
foo: msgFn
})
})

test('function + locale messages', () => {
const functions = Object.create(null)
const msg1 = () => {}
const msg2 = () => {}
functions[generateFormatCacheKey('en', 'hello', 'hello,world')] = msg1
functions[
generateFormatCacheKey('ja', 'hello.hello', 'こんにちは、世界')
] = msg2
const options = {
__i18n: () => ({ functions }),
messages: {
en: { foo: 'foo' },
ja: { foo: 'ふー' }
}
}
const { messages } = createComposer(
options as ComposerOptions<VueMessageType>
)
expect(messages.value.en).toEqual({
hello: msg1,
foo: 'foo'
})
expect(messages.value.ja).toEqual({
hello: {
hello: msg2
},
foo: 'ふー'
})
})
})

describe('__transrateVNode', () => {
Expand Down