Skip to content

Commit 0eb6a4d

Browse files
authored
fix: Built-in components bugs (#723)
closes #722
1 parent ca26ff9 commit 0eb6a4d

File tree

2 files changed

+162
-1
lines changed

2 files changed

+162
-1
lines changed

packages/vue-i18n/src/i18n.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ function getComposer<
700700
.__composer as Composer<Messages, DateTimeFormats, NumberFormats>
701701
}
702702
// eslint-disable-next-line @typescript-eslint/no-explicit-any
703-
if (useComponent && !(composer as any)[InejctWithOption]) {
703+
if (useComponent && composer && !(composer as any)[InejctWithOption]) {
704704
composer = null
705705
}
706706
}

packages/vue-i18n/test/i18n.test.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import {
6+
ref,
67
defineComponent,
78
nextTick,
89
getCurrentInstance,
@@ -960,3 +961,163 @@ test('issue #708', async () => {
960961
`<div> C1: <div>Hello world!</div><div>Hello <strong>world!</strong></div><br><div>C2 slot: <div>Hello world!</div><div>Hello <strong>world!</strong></div></div></div>`
961962
)
962963
})
964+
965+
describe('issue #722', () => {
966+
test('legacy', async () => {
967+
const messages = {
968+
en: { language: 'English' },
969+
ja: { language: '日本語' }
970+
}
971+
972+
const i18n = createI18n({
973+
legacy: true,
974+
locale: 'en',
975+
messages
976+
})
977+
978+
const App = defineComponent({
979+
template: `<transition name="fade">
980+
<i18n-t keypath="hello" tag="p">
981+
<template #world>
982+
<b>{{ $t("world") }}</b>
983+
</template>
984+
</i18n-t>
985+
</transition>`,
986+
i18n: {
987+
messages: {
988+
en: {
989+
hello: 'Hello {world}',
990+
world: 'world!'
991+
}
992+
}
993+
}
994+
})
995+
const wrapper = await mount(App, i18n)
996+
997+
expect(wrapper.html()).toEqual(`<p>Hello <b>world!</b></p>`)
998+
})
999+
1000+
test('composition', async () => {
1001+
const messages = {
1002+
en: { language: 'English' },
1003+
ja: { language: '日本語' }
1004+
}
1005+
1006+
const i18n = createI18n({
1007+
legacy: false,
1008+
globalInjection: true,
1009+
locale: 'en',
1010+
messages
1011+
})
1012+
1013+
const App = defineComponent({
1014+
setup() {
1015+
const { t } = useI18n({
1016+
inheritLocale: true,
1017+
messages: {
1018+
en: {
1019+
hello: 'Hello {world}',
1020+
world: 'world!'
1021+
}
1022+
}
1023+
})
1024+
return { t }
1025+
},
1026+
template: `<transition name="fade">
1027+
<i18n-t keypath="hello" tag="p">
1028+
<template #world>
1029+
<b>{{ t("world") }}</b>
1030+
</template>
1031+
</i18n-t>
1032+
</transition>`
1033+
})
1034+
const wrapper = await mount(App, i18n)
1035+
1036+
expect(wrapper.html()).toEqual(`<p>Hello <b>world!</b></p>`)
1037+
})
1038+
1039+
test('v-if: legacy', async () => {
1040+
const messages = {
1041+
en: { language: 'English' },
1042+
ja: { language: '日本語' }
1043+
}
1044+
1045+
const i18n = createI18n({
1046+
legacy: true,
1047+
locale: 'en',
1048+
messages
1049+
})
1050+
1051+
const App = defineComponent({
1052+
data() {
1053+
return { flag: true }
1054+
},
1055+
template: `<div v-if="flag">
1056+
<i18n-t keypath="hello" tag="p">
1057+
<template #world>
1058+
<b>{{ $t("world") }}</b>
1059+
</template>
1060+
</i18n-t>
1061+
</div>`,
1062+
i18n: {
1063+
messages: {
1064+
en: {
1065+
hello: 'Hello {world}',
1066+
world: 'world!'
1067+
}
1068+
}
1069+
}
1070+
})
1071+
const wrapper = await mount(App, i18n)
1072+
1073+
expect(wrapper.html()).toEqual(`<div><p>Hello <b>world!</b></p></div>`)
1074+
})
1075+
1076+
test('v-if: composition', async () => {
1077+
const messages = {
1078+
en: { language: 'English' },
1079+
ja: { language: '日本語' }
1080+
}
1081+
1082+
const i18n = createI18n({
1083+
legacy: false,
1084+
globalInjection: true,
1085+
locale: 'en',
1086+
messages
1087+
})
1088+
1089+
const App = defineComponent({
1090+
setup() {
1091+
const { t } = useI18n({
1092+
inheritLocale: true,
1093+
messages: {
1094+
en: {
1095+
hello: 'Hello {world}',
1096+
world: 'world!'
1097+
}
1098+
}
1099+
})
1100+
const flag = ref(true)
1101+
return { t, flag }
1102+
},
1103+
template: `<div v-if="flag">
1104+
<i18n-t keypath="hello" tag="p">
1105+
<template #world>
1106+
<b>{{ t("world") }}</b>
1107+
</template>
1108+
</i18n-t>
1109+
</div>`,
1110+
i18n: {
1111+
messages: {
1112+
en: {
1113+
hello: 'Hello {world}',
1114+
world: 'world!'
1115+
}
1116+
}
1117+
}
1118+
})
1119+
const wrapper = await mount(App, i18n)
1120+
1121+
expect(wrapper.html()).toEqual(`<div><p>Hello <b>world!</b></p></div>`)
1122+
})
1123+
})

0 commit comments

Comments
 (0)