diff --git a/packages/vue-i18n/src/i18n.ts b/packages/vue-i18n/src/i18n.ts index 0fcd8ee05..80b86735e 100644 --- a/packages/vue-i18n/src/i18n.ts +++ b/packages/vue-i18n/src/i18n.ts @@ -700,7 +700,7 @@ function getComposer< .__composer as Composer } // eslint-disable-next-line @typescript-eslint/no-explicit-any - if (useComponent && !(composer as any)[InejctWithOption]) { + if (useComponent && composer && !(composer as any)[InejctWithOption]) { composer = null } } diff --git a/packages/vue-i18n/test/i18n.test.ts b/packages/vue-i18n/test/i18n.test.ts index 02a69a81f..6cb88178f 100644 --- a/packages/vue-i18n/test/i18n.test.ts +++ b/packages/vue-i18n/test/i18n.test.ts @@ -3,6 +3,7 @@ */ import { + ref, defineComponent, nextTick, getCurrentInstance, @@ -960,3 +961,163 @@ test('issue #708', async () => { `
C1:
Hello world!
Hello world!

C2 slot:
Hello world!
Hello world!
` ) }) + +describe('issue #722', () => { + test('legacy', async () => { + const messages = { + en: { language: 'English' }, + ja: { language: '日本語' } + } + + const i18n = createI18n({ + legacy: true, + locale: 'en', + messages + }) + + const App = defineComponent({ + template: ` + + + +`, + i18n: { + messages: { + en: { + hello: 'Hello {world}', + world: 'world!' + } + } + } + }) + const wrapper = await mount(App, i18n) + + expect(wrapper.html()).toEqual(`

Hello world!

`) + }) + + test('composition', async () => { + const messages = { + en: { language: 'English' }, + ja: { language: '日本語' } + } + + const i18n = createI18n({ + legacy: false, + globalInjection: true, + locale: 'en', + messages + }) + + const App = defineComponent({ + setup() { + const { t } = useI18n({ + inheritLocale: true, + messages: { + en: { + hello: 'Hello {world}', + world: 'world!' + } + } + }) + return { t } + }, + template: ` + + + +` + }) + const wrapper = await mount(App, i18n) + + expect(wrapper.html()).toEqual(`

Hello world!

`) + }) + + test('v-if: legacy', async () => { + const messages = { + en: { language: 'English' }, + ja: { language: '日本語' } + } + + const i18n = createI18n({ + legacy: true, + locale: 'en', + messages + }) + + const App = defineComponent({ + data() { + return { flag: true } + }, + template: `
+ + + +
`, + i18n: { + messages: { + en: { + hello: 'Hello {world}', + world: 'world!' + } + } + } + }) + const wrapper = await mount(App, i18n) + + expect(wrapper.html()).toEqual(`

Hello world!

`) + }) + + test('v-if: composition', async () => { + const messages = { + en: { language: 'English' }, + ja: { language: '日本語' } + } + + const i18n = createI18n({ + legacy: false, + globalInjection: true, + locale: 'en', + messages + }) + + const App = defineComponent({ + setup() { + const { t } = useI18n({ + inheritLocale: true, + messages: { + en: { + hello: 'Hello {world}', + world: 'world!' + } + } + }) + const flag = ref(true) + return { t, flag } + }, + template: `
+ + + +
`, + i18n: { + messages: { + en: { + hello: 'Hello {world}', + world: 'world!' + } + } + } + }) + const wrapper = await mount(App, i18n) + + expect(wrapper.html()).toEqual(`

Hello world!

`) + }) +})