-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathi18n.ts
262 lines (236 loc) · 7.79 KB
/
i18n.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { isObject, isFunction, assign } from '@intlify/shared'
import { ref, computed, watch, isVue3, effectScope, isVue2 } from 'vue-demi'
import {
localePath,
localeRoute,
localeLocation,
switchLocalePath,
getRouteBaseName,
resolveRoute,
localeHead
} from '../compatibles'
import { DEFAULT_BASE_URL } from '../constants'
import { resolveBaseUrl, isVueI18n, getComposer, inBrowser } from '../utils'
import type { I18nRoutingOptions, LocaleObject } from '../types'
import type { I18n, Composer, VueI18n, VueI18nExtender, ComposerExtender, Disposer } from '@intlify/vue-i18n-bridge'
import type { App } from 'vue-demi'
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Vue = any
// eslint-disable-next-line @typescript-eslint/ban-types
export function proxyVueInstance(target: Function): Function {
// `this` is the Vue instance
return function (this: Vue) {
return Reflect.apply(
target,
{
getRouteBaseName: this.getRouteBaseName,
localePath: this.localePath,
localeRoute: this.localeRoute,
localeLocation: this.localeLocation,
resolveRoute: this.resolveRoute,
switchLocalePath: this.switchLocalePath,
localeHead: this.localeHead,
i18n: this.$i18n,
route: this.$route,
router: this.$router
},
// eslint-disable-next-line prefer-rest-params
arguments
)
}
}
/**
* An options of Vue I18n Routing Plugin
*/
export interface VueI18nRoutingPluginOptions {
/**
* Whether to inject some option APIs style methods into Vue instance
*
* @defaultValue `true`
*/
inject?: boolean
/**
* @internal
*/
__composerExtend?: ComposerExtender
/**
* @internal
*/
__vueI18nExtend?: VueI18nExtender
}
export interface ExtendProperyDescripters {
[key: string]: Pick<PropertyDescriptor, 'get'>
}
export type ExtendComposerHook = (compser: Composer) => void
export type ExtendVueI18nHook = (composer: Composer) => ExtendProperyDescripters
export type ExtendExportedGlobalHook = (global: Composer) => ExtendProperyDescripters
export interface ExtendHooks {
onExtendComposer?: ExtendComposerHook
onExtendExportedGlobal?: ExtendExportedGlobalHook
onExtendVueI18n?: ExtendVueI18nHook
}
export type VueI18nExtendOptions<Context = unknown> = Pick<I18nRoutingOptions<Context>, 'baseUrl'> & {
locales?: string[] | LocaleObject[]
localeCodes?: string[]
context?: Context
hooks?: ExtendHooks
}
export function extendI18n<Context = unknown, TI18n extends I18n = I18n>(
i18n: TI18n,
{
locales = [],
localeCodes = [],
baseUrl = DEFAULT_BASE_URL,
hooks = {},
context = {} as Context
}: VueI18nExtendOptions<Context> = {}
) {
const scope = effectScope()
const orgInstall = i18n.install
i18n.install = (vue: Vue, ...options: unknown[]) => {
const pluginOptions = isPluginOptions(options[0]) ? assign({}, options[0]) : { inject: true }
if (pluginOptions.inject == null) {
pluginOptions.inject = true
}
const orgComposerExtend = pluginOptions.__composerExtend
pluginOptions.__composerExtend = (localComposer: Composer) => {
const globalComposer = getComposer(i18n)
localComposer.locales = computed(() => globalComposer.locales.value)
localComposer.localeCodes = computed(() => globalComposer.localeCodes.value)
localComposer.baseUrl = computed(() => globalComposer.baseUrl.value)
let orgComposerDispose: Disposer | undefined
if (isFunction(orgComposerExtend)) {
orgComposerDispose = Reflect.apply(orgComposerExtend, pluginOptions, [localComposer])
}
return () => {
orgComposerDispose && orgComposerDispose()
}
}
if (i18n.mode === 'legacy') {
const orgVueI18nExtend = pluginOptions.__vueI18nExtend
pluginOptions.__vueI18nExtend = (vueI18n: VueI18n) => {
extendVueI18n(vueI18n, hooks.onExtendVueI18n)
let orgVueI18nDispose: Disposer | undefined
if (isFunction(orgVueI18nExtend)) {
orgVueI18nDispose = Reflect.apply(orgVueI18nExtend, pluginOptions, [vueI18n])
}
return () => {
orgVueI18nDispose && orgVueI18nDispose()
}
}
}
options[0] = pluginOptions
Reflect.apply(orgInstall, i18n, [vue, ...options])
const globalComposer = getComposer(i18n)
// extend global
scope.run(() => {
extendComposer(globalComposer, { locales, localeCodes, baseUrl, hooks, context })
if (i18n.mode === 'legacy' && isVueI18n(i18n.global)) {
extendVueI18n(i18n.global, hooks.onExtendVueI18n)
}
})
// extend vue component instance for Vue 3
const app = vue as App
// prettier-ignore
const exported = i18n.mode === 'composition'
? isVue3
? app.config.globalProperties.$i18n
: i18n
// for legacy mode
: isVue2
? i18n
: null
if (exported) {
extendExportedGlobal(exported, globalComposer, hooks.onExtendExportedGlobal)
}
if (pluginOptions.inject) {
// extend vue component instance
vue.mixin({
methods: {
resolveRoute: proxyVueInstance(resolveRoute),
localePath: proxyVueInstance(localePath),
localeRoute: proxyVueInstance(localeRoute),
localeLocation: proxyVueInstance(localeLocation),
switchLocalePath: proxyVueInstance(switchLocalePath),
getRouteBaseName: proxyVueInstance(getRouteBaseName),
localeHead: proxyVueInstance(localeHead)
}
})
}
// dispose when app will be unmounting
if (app.unmount) {
const unmountApp = app.unmount
app.unmount = () => {
scope.stop()
unmountApp()
}
}
}
return scope
}
function extendComposer<Context = unknown>(composer: Composer, options: VueI18nExtendOptions<Context>) {
const { locales, localeCodes, baseUrl, context } = options
const _locales = ref<string[] | LocaleObject[]>(locales!)
const _localeCodes = ref<string[]>(localeCodes!)
const _baseUrl = ref<string>('')
composer.locales = computed(() => _locales.value)
composer.localeCodes = computed(() => _localeCodes.value)
composer.baseUrl = computed(() => _baseUrl.value)
if (inBrowser) {
watch(
composer.locale,
() => {
_baseUrl.value = resolveBaseUrl(baseUrl!, context!)
},
{ immediate: true }
)
} else {
_baseUrl.value = resolveBaseUrl(baseUrl!, context!)
}
if (options.hooks && options.hooks.onExtendComposer) {
options.hooks.onExtendComposer(composer)
}
}
function extendProperyDescripters(
composer: Composer,
exported: any, // eslint-disable-line @typescript-eslint/no-explicit-any
hook?: ExtendVueI18nHook | ExtendExportedGlobalHook
): void {
const properties: ExtendProperyDescripters[] = [
{
locales: {
get() {
return composer.locales.value
}
},
localeCodes: {
get() {
return composer.localeCodes.value
}
},
baseUrl: {
get() {
return composer.baseUrl.value
}
}
}
]
hook && properties.push(hook(composer))
for (const property of properties) {
for (const [key, descriptor] of Object.entries(property)) {
Object.defineProperty(exported, key, descriptor)
}
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function extendExportedGlobal(exported: any, g: Composer, hook?: ExtendExportedGlobalHook) {
extendProperyDescripters(g, exported, hook)
}
function extendVueI18n(vueI18n: VueI18n, hook?: ExtendVueI18nHook): void {
const c = getComposer(vueI18n)
extendProperyDescripters(c, vueI18n, hook)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isPluginOptions(options: any): options is VueI18nRoutingPluginOptions {
return isObject(options) && ('inject' in options || '__composerExtend' in options || '__vueI18nExtend' in options)
}