Skip to content

Commit

Permalink
feat: Add parsePages & pages options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
paulgv committed May 9, 2018
1 parent 998a2c1 commit b2980cf
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const DEFAULT_OPTIONS = {
setMessages: 'I18N_SET_MESSAGES'
}
},
parsePages: true,
pages: {},
beforeLanguageSwitch: () => null,
onLanguageSwitched: () => null
}
Expand Down
28 changes: 23 additions & 5 deletions src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,53 @@ import {
MODULE_NAME,
STRATEGIES } from './constants'
import { extractComponentOptions } from './components'
import { getLocaleCodes } from './utils'
import { getPageOptions, getLocaleCodes } from './utils'

export const 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
const extractedOptions = extractComponentOptions(route.component)
if (parsePages) {
pageOptions = extractComponentOptions(route.component)
} else {
pageOptions = getPageOptions(route, pages, locales, pagesDir)
}

// Skip route if i18n is disabled on page
if (extractedOptions === false) {
if (pageOptions === false) {
return route
}

// Component's specific options
const componentOptions = {
locales,
...extractComponentOptions(route.component),
...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++) {
Expand Down Expand Up @@ -82,7 +100,7 @@ export const makeRoutes = (baseRoutes, {

for (let i = 0, length1 = baseRoutes.length; i < length1; i++) {
const route = baseRoutes[i]
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, locales))
localizedRoutes = localizedRoutes.concat(buildLocalizedRoutes(route, { locales }))
}

return localizedRoutes
Expand Down
37 changes: 37 additions & 0 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,43 @@ export const getLocaleCodes = (locales = []) => {
return []
}

/**
* Retrieve page's options from the module's configuration for a given route
* @param {Object} route Route
* @param {Object} pages Pages options from module's configuration
* @param {Array} locales Locale from module's configuration
* @param {String} pagesDir Pages dir from Nuxt's configuration
* @return {Object} Page options
*/
export const getPageOptions = (route, pages, locales, pagesDir) => {
const options = {
locales: getLocaleCodes(locales),
paths: {}
}
const pattern = new RegExp(`${pagesDir}/`, 'i')
const chunkName = route.chunkName.replace(pattern, '')
const pageOptions = pages[chunkName]
// Routing disabled
if (pageOptions === false) {
return false
}
// Skip if no page options defined
if (!pageOptions) {
return options
}
// Construct options object
Object.keys(pageOptions).forEach((locale) => {
// Remove disabled locales from page options
if (pageOptions[locale] === false) {
options.locales = options.locales.filter(l => l !== locale)
} else if (typeof pageOptions[locale] === 'string') {
// Set custom path if any
options.paths[locale] = pageOptions[locale]
}
})
return options
}

/**
* Extract locale code from given route:
* - If route has a name, try to extract locale from it
Expand Down
5 changes: 4 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export default function (userOptions) {

// Generate localized routes
this.extendRoutes((routes) => {
const localizedRoutes = makeRoutes(routes, options)
const localizedRoutes = makeRoutes(routes, {
...options,
pagesDir: this.options.dir.pages
})
routes.splice(0, routes.length)
routes.unshift(...localizedRoutes)
})
Expand Down

0 comments on commit b2980cf

Please sign in to comment.