forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
50 lines (48 loc) · 1.61 KB
/
routes.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
const { has } = require('lodash')
/**
* Generate localized route using Nuxt's generated routes and i18n config
* @param {Array} baseRoutes Nuxt's default routes based on pages/ directory
* @param {Array} locales Locales to use for route generation, should be
* used when recursively generating children routes,
* defaults to app's configured LOCALES
* @return {Array} Localized routes to be used in Nuxt config
*/
const generateRoutes = ({ baseRoutes, locales, defaultLocale, routesOptions }) => {
// Abort routes generation if no routes or locales specified
if (!baseRoutes || !locales) {
return []
}
const newRoutes = []
baseRoutes.forEach((baseRoute) => {
locales.forEach((locale) => {
const { component } = baseRoute
let { path, name, children } = baseRoute
if (children) {
children = generateRoutes(children, [locale])
}
const { code } = locale
if (has(routesOptions, `${name}.${code}`)) {
path = routesOptions[name][code]
}
if (code !== defaultLocale) {
// Add leading / if needed (ie. children routes)
if (path.match(/^\//) === null) {
path = `/${path}`
}
// Prefix path with locale code if not default locale
path = `/${code}${path}`
}
const route = { path, component }
if (name) {
name += `-${code}`
route.name = name
}
if (children) {
route.children = children
}
newRoutes.push(route)
})
})
return newRoutes
}
module.exports = { generateRoutes }