-
Notifications
You must be signed in to change notification settings - Fork 463
/
routes.js
115 lines (97 loc) · 3.59 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
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
const {
MODULE_NAME,
STRATEGIES } = require('./constants')
const { extractComponentOptions } = require('./components')
const { getPageOptions, getLocaleCodes } = require('./utils')
exports.makeRoutes = (baseRoutes, {
locales,
defaultLocale,
routesNameSeparator,
strategy,
parsePages,
pages,
pagesDir,
differentDomains
}) => {
locales = getLocaleCodes(locales)
let localizedRoutes = []
const buildLocalizedRoutes = (route, routeOptions = {}, isChild = false) => {
const routes = []
let pageOptions
// Extract i18n options from page
if (parsePages) {
pageOptions = extractComponentOptions(route.component)
} else {
pageOptions = getPageOptions(route, pages, locales, pagesDir)
}
// Skip route if i18n is disabled on page
if (pageOptions === false) {
return route
}
// Component's specific options
const componentOptions = {
locales,
...pageOptions,
...routeOptions
}
// Double check locales to remove any locales not found in pageOptions
// This is there to prevent children routes being localized even though
// they are disabled in the configuration
if (
typeof componentOptions.locales !== 'undefined' && componentOptions.locales.length > 0 &&
typeof pageOptions.locales !== 'undefined' && pageOptions.locales.length > 0) {
componentOptions.locales = componentOptions.locales.filter((locale) => (
pageOptions.locales.indexOf(locale) !== -1
))
}
// Generate routes for component's supported locales
for (let i = 0, length1 = componentOptions.locales.length; i < length1; i++) {
const locale = componentOptions.locales[i]
let { name, path } = route
const localizedRoute = { ...route }
// Skip if locale not in module's configuration
if (locales.indexOf(locale) === -1) {
// eslint-disable-next-line
console.warn(`[${MODULE_NAME}] Can't generate localized route for route '${name}' with locale '${locale}' because locale is not in the module's configuration`)
continue
}
// Make localized route name
localizedRoute.name = name + routesNameSeparator + locale
// Generate localized children routes if any
if (route.children) {
delete localizedRoute.name
localizedRoute.children = []
for (let i = 0, length1 = route.children.length; i < length1; i++) {
localizedRoute.children = localizedRoute.children.concat(buildLocalizedRoutes(route.children[i], { locales: [locale] }, true))
}
}
// Get custom path if any
if (componentOptions.paths && componentOptions.paths[locale]) {
path = encodeURI(componentOptions.paths[locale])
}
// Add route prefix if needed
const shouldAddPrefix = (
// No prefix if app uses different locale domains
!differentDomains &&
// Only add prefix on top level routes
!isChild &&
// Skip default locale if strategy is PREFIX_EXCEPT_DEFAULT
!(locale === defaultLocale && strategy === STRATEGIES.PREFIX_EXCEPT_DEFAULT)
)
if (locale === defaultLocale && strategy === STRATEGIES.PREFIX_AND_DEFAULT) {
routes.push({ ...localizedRoute, path })
}
if (shouldAddPrefix) {
path = `/${locale}${path}`
}
localizedRoute.path = path
routes.push(localizedRoute)
}
return routes
}
for (let i = 0, length1 = baseRoutes.length; i < length1; i++) {
const route = baseRoutes[i]
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, { locales }))
}
return localizedRoutes
}