Skip to content

Commit 7e59b05

Browse files
authored
fix: multiple VueI18n extending (#1239)
1 parent ebb3aab commit 7e59b05

File tree

2 files changed

+53
-18
lines changed

2 files changed

+53
-18
lines changed

packages/vue-i18n-core/src/mixins/next.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ export function defineMixin(
9999
this.$i18n.tm(key)
100100

101101
// extend vue-i18n legacy APIs
102-
if (i18n.__vueI18nExtend) {
102+
if (i18n.__vueI18nExtend && !this.$i18n.__extened__) {
103103
i18n.__vueI18nExtend(this.$i18n)
104+
this.$i18n.__extened__ = true
104105
}
105106
},
106107

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

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ describe('Composer & VueI18n extend hooking', () => {
13681368
const composerExtendSpy = jest
13691369
.fn()
13701370
.mockImplementation((composer: Composer) => {
1371+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13711372
;(composer as any).foo = ref('hello world')
13721373
})
13731374
const vueI18nExtendSpy = jest.fn()
@@ -1389,31 +1390,64 @@ describe('Composer & VueI18n extend hooking', () => {
13891390
pluginOptions: {
13901391
__composerExtend: composerExtendSpy,
13911392
__vueI18nExtend: vueI18nExtendSpy
1392-
} as any
1393+
} as any // eslint-disable-line @typescript-eslint/no-explicit-any
13931394
})
13941395
expect(composerExtendSpy).toHaveBeenCalled()
13951396
expect(html()).toBe('<p>hello world</p>')
13961397
expect(vueI18nExtendSpy).not.toHaveBeenCalled()
13971398
})
13981399

1399-
test('legacy', async () => {
1400-
const composerExtendSpy = jest.fn()
1401-
const vueI18nExtendSpy = jest
1402-
.fn()
1403-
.mockImplementation((vueI18n: VueI18n) => {
1404-
;(vueI18n as any).foo = 'hello world'
1400+
describe('legacy', () => {
1401+
test('basic', async () => {
1402+
const composerExtendSpy = jest.fn()
1403+
const vueI18nExtendSpy = jest
1404+
.fn()
1405+
.mockImplementation((vueI18n: VueI18n) => {
1406+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1407+
;(vueI18n as any).foo = 'hello world'
1408+
})
1409+
const i18n = createI18n({ legacy: true })
1410+
1411+
const App = defineComponent({ template: '<p>{{ $i18n.foo }}</p>' })
1412+
const { html } = await mount(App, i18n, {
1413+
pluginOptions: {
1414+
__composerExtend: composerExtendSpy,
1415+
__vueI18nExtend: vueI18nExtendSpy
1416+
} as any // eslint-disable-line @typescript-eslint/no-explicit-any
14051417
})
1406-
const i18n = createI18n({ legacy: true })
1418+
expect(composerExtendSpy).not.toHaveBeenCalled()
1419+
expect(vueI18nExtendSpy).toHaveBeenCalled()
1420+
expect(html()).toBe('<p>hello world</p>')
1421+
})
14071422

1408-
const App = defineComponent({ template: '<p>{{ $i18n.foo }}</p>' })
1409-
const { html } = await mount(App, i18n, {
1410-
pluginOptions: {
1411-
__composerExtend: composerExtendSpy,
1412-
__vueI18nExtend: vueI18nExtendSpy
1413-
} as any
1423+
test('use global vue i18n instance in components', async () => {
1424+
const composerExtendSpy = jest.fn()
1425+
const vueI18nExtendSpy = jest
1426+
.fn()
1427+
.mockImplementation((vueI18n: VueI18n) => {
1428+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1429+
;(vueI18n as any).foo = 'hello world'
1430+
})
1431+
const i18n = createI18n({ legacy: true })
1432+
1433+
const Child = defineComponent({
1434+
template: '<span>{{ $i18n.foo }}</span>'
1435+
})
1436+
const App = defineComponent({
1437+
components: {
1438+
Child
1439+
},
1440+
template: '<p>{{ $i18n.foo }}</p><Child />'
1441+
})
1442+
const { html } = await mount(App, i18n, {
1443+
pluginOptions: {
1444+
__composerExtend: composerExtendSpy,
1445+
__vueI18nExtend: vueI18nExtendSpy
1446+
} as any // eslint-disable-line @typescript-eslint/no-explicit-any
1447+
})
1448+
expect(composerExtendSpy).not.toHaveBeenCalled()
1449+
expect(vueI18nExtendSpy).toHaveBeenCalledTimes(1)
1450+
expect(html()).toBe('<p>hello world</p><span>hello world</span>')
14141451
})
1415-
expect(composerExtendSpy).not.toHaveBeenCalled()
1416-
expect(vueI18nExtendSpy).toHaveBeenCalled()
1417-
expect(html()).toBe('<p>hello world</p>')
14181452
})
14191453
})

0 commit comments

Comments
 (0)