Skip to content

Commit b2980cf

Browse files
committed
feat: Add parsePages & pages options
Added parePages option that can be set to false to disable acorn parsing to retrieve page specific options, pages option can be used as a substitute to customize routes in the module's configuration
1 parent 998a2c1 commit b2980cf

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

src/helpers/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export const DEFAULT_OPTIONS = {
3939
setMessages: 'I18N_SET_MESSAGES'
4040
}
4141
},
42+
parsePages: true,
43+
pages: {},
4244
beforeLanguageSwitch: () => null,
4345
onLanguageSwitched: () => null
4446
}

src/helpers/routes.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,53 @@ import {
22
MODULE_NAME,
33
STRATEGIES } from './constants'
44
import { extractComponentOptions } from './components'
5-
import { getLocaleCodes } from './utils'
5+
import { getPageOptions, getLocaleCodes } from './utils'
66

77
export const makeRoutes = (baseRoutes, {
88
locales,
99
defaultLocale,
1010
routesNameSeparator,
1111
strategy,
12+
parsePages,
13+
pages,
14+
pagesDir,
1215
differentDomains
1316
}) => {
1417
locales = getLocaleCodes(locales)
1518
let localizedRoutes = []
1619

1720
const buildLocalizedRoutes = (route, routeOptions = {}, isChild = false) => {
1821
const routes = []
22+
let pageOptions
1923

2024
// Extract i18n options from page
21-
const extractedOptions = extractComponentOptions(route.component)
25+
if (parsePages) {
26+
pageOptions = extractComponentOptions(route.component)
27+
} else {
28+
pageOptions = getPageOptions(route, pages, locales, pagesDir)
29+
}
2230

2331
// Skip route if i18n is disabled on page
24-
if (extractedOptions === false) {
32+
if (pageOptions === false) {
2533
return route
2634
}
2735

2836
// Component's specific options
2937
const componentOptions = {
3038
locales,
31-
...extractComponentOptions(route.component),
39+
...pageOptions,
3240
...routeOptions
3341
}
42+
// Double check locales to remove any locales not found in pageOptions
43+
// This is there to prevent children routes being localized even though
44+
// they are disabled in the configuration
45+
if (
46+
typeof componentOptions.locales !== 'undefined' && componentOptions.locales.length > 0 &&
47+
typeof pageOptions.locales !== 'undefined' && pageOptions.locales.length > 0) {
48+
componentOptions.locales = componentOptions.locales.filter((locale) => (
49+
pageOptions.locales.indexOf(locale) !== -1
50+
))
51+
}
3452

3553
// Generate routes for component's supported locales
3654
for (let i = 0, length1 = componentOptions.locales.length; i < length1; i++) {
@@ -82,7 +100,7 @@ export const makeRoutes = (baseRoutes, {
82100

83101
for (let i = 0, length1 = baseRoutes.length; i < length1; i++) {
84102
const route = baseRoutes[i]
85-
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, locales))
103+
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, { locales }))
86104
}
87105

88106
return localizedRoutes

src/helpers/utils.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,43 @@ export const getLocaleCodes = (locales = []) => {
2121
return []
2222
}
2323

24+
/**
25+
* Retrieve page's options from the module's configuration for a given route
26+
* @param {Object} route Route
27+
* @param {Object} pages Pages options from module's configuration
28+
* @param {Array} locales Locale from module's configuration
29+
* @param {String} pagesDir Pages dir from Nuxt's configuration
30+
* @return {Object} Page options
31+
*/
32+
export const getPageOptions = (route, pages, locales, pagesDir) => {
33+
const options = {
34+
locales: getLocaleCodes(locales),
35+
paths: {}
36+
}
37+
const pattern = new RegExp(`${pagesDir}/`, 'i')
38+
const chunkName = route.chunkName.replace(pattern, '')
39+
const pageOptions = pages[chunkName]
40+
// Routing disabled
41+
if (pageOptions === false) {
42+
return false
43+
}
44+
// Skip if no page options defined
45+
if (!pageOptions) {
46+
return options
47+
}
48+
// Construct options object
49+
Object.keys(pageOptions).forEach((locale) => {
50+
// Remove disabled locales from page options
51+
if (pageOptions[locale] === false) {
52+
options.locales = options.locales.filter(l => l !== locale)
53+
} else if (typeof pageOptions[locale] === 'string') {
54+
// Set custom path if any
55+
options.paths[locale] = pageOptions[locale]
56+
}
57+
})
58+
return options
59+
}
60+
2461
/**
2562
* Extract locale code from given route:
2663
* - If route has a name, try to extract locale from it

src/module.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export default function (userOptions) {
5252

5353
// Generate localized routes
5454
this.extendRoutes((routes) => {
55-
const localizedRoutes = makeRoutes(routes, options)
55+
const localizedRoutes = makeRoutes(routes, {
56+
...options,
57+
pagesDir: this.options.dir.pages
58+
})
5659
routes.splice(0, routes.length)
5760
routes.unshift(...localizedRoutes)
5861
})

0 commit comments

Comments
 (0)