Skip to content

Commit a7af4d5

Browse files
committed
improvement/flag-for-vue-i18n-bridge
1 parent 4ad8dbe commit a7af4d5

File tree

6 files changed

+20
-13
lines changed

6 files changed

+20
-13
lines changed

packages/vue-i18n-core/src/composer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ import {
4949
EnableEmitter,
5050
DisableEmitter,
5151
SetPluralRulesSymbol,
52-
LegacyInstanceSymbol
52+
LegacyInstanceSymbol,
53+
__VUE_I18N_BRIDGE__
5354
} from './symbols'
5455
import { deepCopy, getLocaleMessages, getComponentOptions } from './utils'
5556
import { VERSION } from './misc'
@@ -1905,7 +1906,8 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
19051906
? _warnHtmlMessage
19061907
? 'warn'
19071908
: 'off'
1908-
: 'off'
1909+
: 'off',
1910+
__VUE_I18N_BRIDGE__
19091911
}
19101912
__legacy = new VueI18nLegacy(legacyOptions)
19111913
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import { createComposer } from './composer'
2020
import { createVueI18n } from './legacy'
2121
import { I18nWarnCodes, getWarnMessage } from './warnings'
2222
import { I18nErrorCodes, createI18nError } from './errors'
23-
import { EnableEmitter, DisableEmitter, LegacyInstanceSymbol } from './symbols'
23+
import {
24+
EnableEmitter,
25+
DisableEmitter,
26+
LegacyInstanceSymbol,
27+
__VUE_I18N_BRIDGE__
28+
} from './symbols'
2429
import { apply } from './plugin'
2530
import { defineMixin as defineMixinNext } from './mixins/next'
2631
import { defineMixin as defineMixinBridge } from './mixins/bridge'
@@ -769,7 +774,7 @@ export function useI18n<
769774
* @VueI18nGeneral
770775
*/
771776
export const castToVueI18n = /* #__PURE__*/ (i18n: I18n): VueI18n => {
772-
if (!isLegacyVueI18n(i18n)) {
777+
if (!(__VUE_I18N_BRIDGE__ in i18n)) {
773778
throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N)
774779
}
775780
return i18n as unknown as VueI18n

packages/vue-i18n-core/src/legacy.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { createComposer, DefineLocaleMessage } from './composer'
33
import { I18nWarnCodes, getWarnMessage } from './warnings'
44
import { createI18nError, I18nErrorCodes } from './errors'
5-
import { EnableEmitter, DisableEmitter } from './symbols'
5+
import { EnableEmitter, DisableEmitter, __VUE_I18N_BRIDGE__ } from './symbols'
66
import { DEFAULT_LOCALE } from '@intlify/core-base'
77
import {
88
isString,
@@ -15,7 +15,6 @@ import {
1515
assign,
1616
warn
1717
} from '@intlify/shared'
18-
import { isLegacyVueI18n } from './utils'
1918

2019
import type {
2120
Path,
@@ -1464,9 +1463,7 @@ export function createVueI18n(options: any = {}, VueI18nLegacy?: any): any {
14641463
type Message = VueMessageType
14651464

14661465
if (__BRIDGE__) {
1467-
if (!isLegacyVueI18n(VueI18nLegacy)) {
1468-
throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N)
1469-
}
1466+
options[__VUE_I18N_BRIDGE__] = __VUE_I18N_BRIDGE__ // marking
14701467
return new VueI18nLegacy(options)
14711468
} else {
14721469
const composer = createComposer(convertComposerOptions(options)) as Composer

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function defineMixin(
3737
? options.i18n.messages
3838
: {}
3939
;(options.__i18n as string[]).forEach(resource =>
40-
deepCopy(localeMessages, JSON.parse(resource))
40+
deepCopy(JSON.parse(resource), localeMessages)
4141
)
4242
Object.keys(localeMessages).forEach((locale: Locale) => {
4343
options.i18n.mergeLocaleMessage(locale, localeMessages[locale])
@@ -82,7 +82,7 @@ export function defineMixin(
8282
? options.i18n.messages
8383
: {}
8484
;(options.__i18n as string[]).forEach(resource =>
85-
deepCopy(localeMessages, JSON.parse(resource))
85+
deepCopy(JSON.parse(resource), localeMessages)
8686
)
8787
options.i18n.messages = localeMessages
8888
} catch (e) {
@@ -94,7 +94,7 @@ export function defineMixin(
9494

9595
const { sharedMessages } = options.i18n
9696
if (sharedMessages && isPlainObject(sharedMessages)) {
97-
deepCopy(options.i18n.messages, sharedMessages)
97+
deepCopy(sharedMessages, options.i18n.messages)
9898
}
9999

100100
this._i18n = new VueI18n(options.i18n)

packages/vue-i18n-core/src/symbols.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ export const DisableEmitter = /* #__PURE__*/ makeSymbol('__disableEmitter')
99
export const SetPluralRulesSymbol = makeSymbol('__setPluralRules')
1010
export const DevToolsMetaSymbol = makeSymbol('__intlifyMeta')
1111
export const LegacyInstanceSymbol = /* #__PURE__*/ makeSymbol('__legacyVueI18n')
12+
13+
export const __VUE_I18N_BRIDGE__ = /* #__PURE__*/ '__VUE_I18N_BRIDGE__'

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
import { createEmitter } from '@intlify/shared'
2323
import { mount, pluralRules as _pluralRules } from './helper'
2424
import { createI18n, useI18n, castToVueI18n } from '../src/i18n'
25+
import { __VUE_I18N_BRIDGE__ } from '../src/symbols'
2526
import { errorMessages, I18nErrorCodes } from '../src/errors'
2627
import { Composer } from '../src/composer'
2728

@@ -942,7 +943,7 @@ test('Intlify devtools hooking', () => {
942943
describe('castToVueI18n', () => {
943944
test('succeeded', () => {
944945
const mockVueI18n = {
945-
version: '8.2.0'
946+
__VUE_I18N_BRIDGE__
946947
} as unknown as I18n
947948
expect(castToVueI18n(mockVueI18n)).toBe(mockVueI18n)
948949
})

0 commit comments

Comments
 (0)