@@ -14,7 +14,7 @@ import { DEFAULT_BASE_URL } from '../constants'
14
14
import { resolveBaseUrl , isVueI18n , getComposer , inBrowser } from '../utils'
15
15
16
16
import type { I18nRoutingOptions , LocaleObject } from '../types'
17
- import type { I18n , Composer , VueI18n } from '@intlify/vue-i18n-bridge'
17
+ import type { I18n , Composer , VueI18n , VueI18nExtender , ComposerExtender , Disposer } from '@intlify/vue-i18n-bridge'
18
18
import type { App } from 'vue-demi'
19
19
20
20
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -57,11 +57,11 @@ export interface VueI18nRoutingPluginOptions {
57
57
/**
58
58
* @internal
59
59
*/
60
- __composerExtend ?: ( composer : Composer ) => void
60
+ __composerExtend ?: ComposerExtender
61
61
/**
62
62
* @internal
63
63
*/
64
- __vueI18nExtend ?: ( vueI18n : VueI18n ) => void
64
+ __vueI18nExtend ?: VueI18nExtender
65
65
}
66
66
67
67
export interface ExtendProperyDescripters {
@@ -103,35 +103,45 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
103
103
pluginOptions . inject = true
104
104
}
105
105
const orgComposerExtend = pluginOptions . __composerExtend
106
- pluginOptions . __composerExtend = ( c : Composer ) => {
107
- const g = getComposer ( i18n )
108
- c . locales = computed ( ( ) => g . locales . value )
109
- c . localeCodes = computed ( ( ) => g . localeCodes . value )
110
- c . baseUrl = computed ( ( ) => g . baseUrl . value )
106
+ pluginOptions . __composerExtend = ( localComposer : Composer ) => {
107
+ const globalComposer = getComposer ( i18n )
108
+ localComposer . locales = computed ( ( ) => globalComposer . locales . value )
109
+ localComposer . localeCodes = computed ( ( ) => globalComposer . localeCodes . value )
110
+ localComposer . baseUrl = computed ( ( ) => globalComposer . baseUrl . value )
111
+ let orgComposerDispose : Disposer | undefined
111
112
if ( isFunction ( orgComposerExtend ) ) {
112
- Reflect . apply ( orgComposerExtend , pluginOptions , [ c ] )
113
+ orgComposerDispose = Reflect . apply ( orgComposerExtend , pluginOptions , [ globalComposer ] )
114
+ }
115
+ return ( ) => {
116
+ orgComposerDispose && orgComposerDispose ( )
113
117
}
114
118
}
115
- if ( isVueI18n ( i18n . global ) ) {
119
+ if ( i18n . mode === 'legacy' ) {
116
120
const orgVueI18nExtend = pluginOptions . __vueI18nExtend
117
121
pluginOptions . __vueI18nExtend = ( vueI18n : VueI18n ) => {
118
122
extendVueI18n ( vueI18n , hooks . onExtendVueI18n )
123
+ let orgVueI18nDispose : Disposer | undefined
119
124
if ( isFunction ( orgVueI18nExtend ) ) {
120
- Reflect . apply ( orgVueI18nExtend , pluginOptions , [ vueI18n ] )
125
+ orgVueI18nDispose = Reflect . apply ( orgVueI18nExtend , pluginOptions , [ vueI18n ] )
126
+ }
127
+ return ( ) => {
128
+ orgVueI18nDispose && orgVueI18nDispose ( )
121
129
}
122
130
}
123
131
}
124
132
125
133
options [ 0 ] = pluginOptions
126
134
Reflect . apply ( orgInstall , i18n , [ vue , ...options ] )
127
135
128
- const composer = getComposer ( i18n )
136
+ const globalComposer = getComposer ( i18n )
129
137
130
138
// extend global
131
- scope . run ( ( ) => extendComposer ( composer , { locales, localeCodes, baseUrl, hooks, context } ) )
132
- if ( isVueI18n ( i18n . global ) ) {
133
- extendVueI18n ( i18n . global , hooks . onExtendVueI18n )
134
- }
139
+ scope . run ( ( ) => {
140
+ extendComposer ( globalComposer , { locales, localeCodes, baseUrl, hooks, context } )
141
+ if ( i18n . mode === 'legacy' && isVueI18n ( i18n . global ) ) {
142
+ extendVueI18n ( i18n . global , hooks . onExtendVueI18n )
143
+ }
144
+ } )
135
145
136
146
// extend vue component instance for Vue 3
137
147
const app = vue as App
@@ -140,11 +150,12 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
140
150
? isVue3
141
151
? app . config . globalProperties . $i18n
142
152
: i18n
153
+ // for legacy mode
143
154
: isVue2
144
155
? i18n
145
156
: null
146
157
if ( exported ) {
147
- extendExportedGlobal ( exported , composer , hooks . onExtendExportedGlobal )
158
+ extendExportedGlobal ( exported , globalComposer , hooks . onExtendExportedGlobal )
148
159
}
149
160
150
161
if ( pluginOptions . inject ) {
@@ -162,7 +173,7 @@ export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
162
173
} )
163
174
}
164
175
165
- // release scope on unmounting
176
+ // dispose when app will be unmounting
166
177
if ( app . unmount ) {
167
178
const unmountApp = app . unmount
168
179
app . unmount = ( ) => {
@@ -203,37 +214,11 @@ function extendComposer<Context = unknown>(composer: Composer, options: VueI18nE
203
214
}
204
215
}
205
216
206
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
- function extendExportedGlobal ( exported : any , g : Composer , hook ?: ExtendExportedGlobalHook ) {
208
- const properties : ExtendProperyDescripters [ ] = [
209
- {
210
- locales : {
211
- get ( ) {
212
- return g . locales . value
213
- }
214
- } ,
215
- localeCodes : {
216
- get ( ) {
217
- return g . localeCodes . value
218
- }
219
- } ,
220
- baseUrl : {
221
- get ( ) {
222
- return g . baseUrl . value
223
- }
224
- }
225
- }
226
- ]
227
- hook && properties . push ( hook ( g ) )
228
- for ( const property of properties ) {
229
- for ( const [ key , descriptor ] of Object . entries ( property ) ) {
230
- Object . defineProperty ( exported , key , descriptor )
231
- }
232
- }
233
- }
234
-
235
- function extendVueI18n ( vueI18n : VueI18n , hook ?: ExtendVueI18nHook ) : void {
236
- const composer = getComposer ( vueI18n )
217
+ function extendProperyDescripters (
218
+ composer : Composer ,
219
+ exported : any , // eslint-disable-line @typescript-eslint/no-explicit-any
220
+ hook ?: ExtendVueI18nHook | ExtendExportedGlobalHook
221
+ ) : void {
237
222
const properties : ExtendProperyDescripters [ ] = [
238
223
{
239
224
locales : {
@@ -256,11 +241,21 @@ function extendVueI18n(vueI18n: VueI18n, hook?: ExtendVueI18nHook): void {
256
241
hook && properties . push ( hook ( composer ) )
257
242
for ( const property of properties ) {
258
243
for ( const [ key , descriptor ] of Object . entries ( property ) ) {
259
- Object . defineProperty ( vueI18n , key , descriptor )
244
+ Object . defineProperty ( exported , key , descriptor )
260
245
}
261
246
}
262
247
}
263
248
249
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
250
+ function extendExportedGlobal ( exported : any , g : Composer , hook ?: ExtendExportedGlobalHook ) {
251
+ extendProperyDescripters ( g , exported , hook )
252
+ }
253
+
254
+ function extendVueI18n ( vueI18n : VueI18n , hook ?: ExtendVueI18nHook ) : void {
255
+ const c = getComposer ( vueI18n )
256
+ extendProperyDescripters ( c , vueI18n , hook )
257
+ }
258
+
264
259
// eslint-disable-next-line @typescript-eslint/no-explicit-any
265
260
function isPluginOptions ( options : any ) : options is VueI18nRoutingPluginOptions {
266
261
return isObject ( options ) && ( 'inject' in options || '__composerExtend' in options || '__vueI18nExtend' in options )
0 commit comments