-
-
Notifications
You must be signed in to change notification settings - Fork 483
/
plugin.routing.js
263 lines (233 loc) · 7.77 KB
/
plugin.routing.js
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
263
import './middleware'
import Vue from 'vue'
import { Constants, nuxtOptions, options } from './options'
import { getDomainFromLocale } from './plugin.utils'
// @ts-ignore
import { withoutTrailingSlash, withTrailingSlash } from '~i18n-ufo'
/**
* @this {import('../../types/internal').PluginProxy}
* @type {Vue['localePath']}
*/
function localePath (route, locale) {
const localizedRoute = resolveRoute.call(this, route, locale)
return localizedRoute ? localizedRoute.route.fullPath : ''
}
/**
* @this {import('../../types/internal').PluginProxy}
* @type {Vue['localeRoute']}
*/
function localeRoute (route, locale) {
const resolved = resolveRoute.call(this, route, locale)
return resolved ? resolved.route : undefined
}
/**
* @this {import('../../types/internal').PluginProxy}
* @type {Vue['localeLocation']}
*/
function localeLocation (route, locale) {
const resolved = resolveRoute.call(this, route, locale)
return resolved ? resolved.location : undefined
}
/**
* @this {import('../../types/internal').PluginProxy}
* @param {import('vue-router').RawLocation} route
* @param {string} [locale]
* @return {ReturnType<import('vue-router').default['resolve']> | undefined}
*/
function resolveRoute (route, locale) {
// Abort if no route or no locale
if (!route) {
return
}
const { i18n } = this
locale = locale || i18n.locale
if (!locale) {
return
}
// If route parameter is a string, check if it's a path or name of route.
if (typeof route === 'string') {
if (route[0] === '/') {
// If route parameter is a path, create route object with path.
route = { path: route }
} else {
// Else use it as route name.
route = { name: route }
}
}
let localizedRoute = Object.assign({}, route)
if (localizedRoute.path && !localizedRoute.name) {
const resolvedRoute = this.router.resolve(localizedRoute).route
const resolvedRouteName = this.getRouteBaseName(resolvedRoute)
if (resolvedRouteName) {
localizedRoute = {
name: getLocaleRouteName(resolvedRouteName, locale),
params: resolvedRoute.params,
query: resolvedRoute.query,
hash: resolvedRoute.hash
}
} else {
const isDefaultLocale = locale === options.defaultLocale
// if route has a path defined but no name, resolve full route using the path
const isPrefixed =
// don't prefix default locale
!(isDefaultLocale && [Constants.STRATEGIES.PREFIX_EXCEPT_DEFAULT, Constants.STRATEGIES.PREFIX_AND_DEFAULT].includes(options.strategy)) &&
// no prefix for any language
!(options.strategy === Constants.STRATEGIES.NO_PREFIX) &&
// no prefix for different domains
!i18n.differentDomains
if (isPrefixed) {
localizedRoute.path = `/${locale}${localizedRoute.path}`
}
localizedRoute.path = nuxtOptions.trailingSlash ? withTrailingSlash(localizedRoute.path, true) : withoutTrailingSlash(localizedRoute.path, true)
}
} else {
if (!localizedRoute.name && !localizedRoute.path) {
localizedRoute.name = this.getRouteBaseName()
}
localizedRoute.name = getLocaleRouteName(localizedRoute.name, locale)
const { params } = localizedRoute
if (params && params['0'] === undefined && params.pathMatch) {
params['0'] = params.pathMatch
}
}
const resolvedRoute = this.router.resolve(localizedRoute)
if (resolvedRoute.route.name) {
return resolvedRoute
}
// If didn't resolve to an existing route then just return resolved route based on original input.
return this.router.resolve(route)
}
/**
* @this {import('../../types/internal').PluginProxy}
* @type {Vue['switchLocalePath']}
*/
function switchLocalePath (locale) {
const name = this.getRouteBaseName()
if (!name) {
return ''
}
const { i18n, route, store } = this
const { params, ...routeCopy } = route
let langSwitchParams = {}
if (options.vuex && options.vuex.syncRouteParams && store) {
langSwitchParams = store.getters[`${options.vuex.moduleName}/localeRouteParams`](locale)
}
const baseRoute = Object.assign({}, routeCopy, {
name,
params: {
...params,
...langSwitchParams,
0: params.pathMatch
}
})
let path = this.localePath(baseRoute, locale)
// Handle different domains
if (i18n.differentDomains) {
const getDomainOptions = {
differentDomains: i18n.differentDomains,
normalizedLocales: options.normalizedLocales
}
const domain = getDomainFromLocale(locale, this.req, getDomainOptions)
if (domain) {
path = domain + path
}
}
return path
}
/**
* @this {import('../../types/internal').PluginProxy}
* @type {Vue['getRouteBaseName']}
*/
function getRouteBaseName (givenRoute) {
const route = givenRoute !== undefined ? givenRoute : this.route
if (!route || !route.name) {
return
}
return route.name.split(options.routesNameSeparator)[0]
}
/**
* @param {string | undefined} routeName
* @param {string} locale
*/
function getLocaleRouteName (routeName, locale) {
let name = routeName + (options.strategy === Constants.STRATEGIES.NO_PREFIX ? '' : options.routesNameSeparator + locale)
if (locale === options.defaultLocale && options.strategy === Constants.STRATEGIES.PREFIX_AND_DEFAULT) {
name += options.routesNameSeparator + options.defaultLocaleRouteNameSuffix
}
return name
}
/**
* @template {(...args: any[]) => any} T
* @param {T} targetFunction
* @return {(this: Vue, ...args: Parameters<T>) => ReturnType<T>}
*/
const VueInstanceProxy = function (targetFunction) {
return function () {
const proxy = {
getRouteBaseName: this.getRouteBaseName,
i18n: this.$i18n,
localePath: this.localePath,
localeRoute: this.localeRoute,
localeLocation: this.localeLocation,
req: process.server ? this.$ssrContext.req : null,
route: this.$route,
router: this.$router,
store: this.$store
}
return targetFunction.call(proxy, ...arguments)
}
}
/**
* @template {(...args: any[]) => any} T
* @param {import('@nuxt/types').Context} context
* @param {T} targetFunction
* @return {(...args: Parameters<T>) => ReturnType<T>}
*/
const NuxtContextProxy = function (context, targetFunction) {
return function () {
const { app, req, route, store } = context
const proxy = {
getRouteBaseName: app.getRouteBaseName,
i18n: app.i18n,
localePath: app.localePath,
localeLocation: app.localeLocation,
localeRoute: app.localeRoute,
req: process.server ? req : null,
route,
router: app.router,
store
}
return targetFunction.call(proxy, ...arguments)
}
}
/** @type {import('vue').PluginObject<void>} */
const plugin = {
install (Vue) {
Vue.mixin({
methods: {
localePath: VueInstanceProxy(localePath),
localeRoute: VueInstanceProxy(localeRoute),
localeLocation: VueInstanceProxy(localeLocation),
switchLocalePath: VueInstanceProxy(switchLocalePath),
getRouteBaseName: VueInstanceProxy(getRouteBaseName)
}
})
}
}
/** @type {import('@nuxt/types').Plugin} */
export default (context) => {
Vue.use(plugin)
const { app, store } = context
app.localePath = context.localePath = NuxtContextProxy(context, localePath)
app.localeRoute = context.localeRoute = NuxtContextProxy(context, localeRoute)
app.localeLocation = context.localeLocation = NuxtContextProxy(context, localeLocation)
app.switchLocalePath = context.switchLocalePath = NuxtContextProxy(context, switchLocalePath)
app.getRouteBaseName = context.getRouteBaseName = NuxtContextProxy(context, getRouteBaseName)
if (store) {
store.localePath = app.localePath
store.localeRoute = app.localeRoute
store.localeLocation = app.localeLocation
store.switchLocalePath = app.switchLocalePath
store.getRouteBaseName = app.getRouteBaseName
}
}