Skip to content

Commit b62257e

Browse files
authored
fix: cannot work correctly v-for on Translation component (#820)
* fix: cannot work correctly v-for on Translation component resolves #819 * refactor
1 parent d42d034 commit b62257e

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

packages/vue-i18n-core/src/components/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Fragment } from 'vue'
2+
import { isArray } from '@intlify/shared'
3+
24
import type { NamedValue } from '@intlify/core-base'
35

46
export function getInterpolateArg(
@@ -7,8 +9,15 @@ export function getInterpolateArg(
79
keys: string[]
810
): NamedValue | unknown[] {
911
if (keys.length === 1 && keys[0] === 'default') {
10-
// default slot only
11-
return slots.default ? slots.default() : []
12+
// default slot with list
13+
const ret = slots.default ? slots.default() : []
14+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
15+
return ret.reduce((slot: any[], current: any) => {
16+
return (slot = [
17+
...slot,
18+
...(isArray(current.children) ? current.children : [current])
19+
])
20+
}, [])
1221
} else {
1322
// named slots
1423
return keys.reduce((arg, key) => {

packages/vue-i18n-core/test/components/Translation.test.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
*/
44

55
import { mount } from '../helper'
6-
import { h, defineComponent, SetupContext, VNodeChild, ref } from 'vue'
6+
import {
7+
h,
8+
defineComponent,
9+
SetupContext,
10+
VNodeChild,
11+
ref,
12+
nextTick
13+
} from 'vue'
714
import {
815
compileToFunction,
916
registerMessageCompiler,
@@ -14,6 +21,7 @@ import {
1421
} from '@intlify/core-base'
1522
import { createI18n, useI18n } from '../../src/index'
1623

24+
import type { Ref } from 'vue'
1725
import type { Path, PathValue, MessageResolver } from '@intlify/core-base'
1826

1927
const messages = {
@@ -22,6 +30,7 @@ const messages = {
2230
language: 'English',
2331
quantity: 'Quantity',
2432
list: 'hello, {0}!',
33+
list_multi: 'hello, {0}! Do you like {1}?',
2534
named: 'hello, {name}!',
2635
linked: '@:message.named How are you?',
2736
plural: 'no bananas | {n} banana | {n} bananas'
@@ -277,6 +286,70 @@ test('message resolver', async () => {
277286
expect(mockMessageResolver.mock.calls[0][1]).toEqual('message.named')
278287
})
279288

289+
test('v-if / v-else', async () => {
290+
const i18n = createI18n({
291+
legacy: false,
292+
locale: 'en',
293+
messages
294+
})
295+
296+
let toggle: Ref<boolean>
297+
const App = defineComponent({
298+
setup() {
299+
useI18n()
300+
toggle = ref(true)
301+
return { toggle }
302+
},
303+
template: `
304+
<i18n-t tag="p" class="name" keypath="message.named">
305+
<template #name>
306+
<span v-if="toggle">kazupon</span>
307+
<span v-else="toggle">kazu_pon</span>
308+
</template>
309+
</i18n-t>
310+
`
311+
})
312+
const wrapper = await mount(App, i18n)
313+
314+
expect(wrapper.html()).toEqual(
315+
`<p class="name">hello, <span>kazupon</span>!</p>`
316+
)
317+
318+
toggle!.value = false
319+
await nextTick()
320+
expect(wrapper.html()).toEqual(
321+
`<p class="name">hello, <span>kazu_pon</span>!</p>`
322+
)
323+
})
324+
325+
test('v-for: issue #819', async () => {
326+
const i18n = createI18n({
327+
legacy: false,
328+
locale: 'en',
329+
messages
330+
})
331+
332+
const App = defineComponent({
333+
setup() {
334+
useI18n()
335+
const values = ref(['kazupon', 'oranges'])
336+
return { values }
337+
},
338+
template: `
339+
<i18n-t keypath="message.list_multi" locale="en">
340+
<span v-for="(value, index) in values" :key="index" class="bold">
341+
{{ value }}
342+
</span>
343+
</i18n-t>
344+
`
345+
})
346+
const wrapper = await mount(App, i18n)
347+
348+
expect(wrapper.html()).toEqual(
349+
`hello, <span class="bold">kazupon</span>! Do you like <span class="bold">oranges</span>?`
350+
)
351+
})
352+
280353
test('issue #708', async () => {
281354
const i18n = createI18n({
282355
legacy: true,

0 commit comments

Comments
 (0)