Skip to content

Commit

Permalink
feat: Use default locale's custom path if not defined for a locale (n…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Philippe Dos Santos authored and paulgv committed Jul 20, 2019
1 parent 20520dc commit d30e5f0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ You would need to set up your `pages` property as follows:
}]
```
If a custom path is missing for one of the locales, the `defaultLocale` custom path is used, if set.
### Regular Expression
By default, all custom paths are encoded to handle non-latin characters in the path. This will convert paths with regular expression like `/foo/:slug-:id(\\d+)` to `/foo/:slug-:id(%5Cd+)`.
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports.makeRoutes = (baseRoutes, {
if (parsePages) {
pageOptions = extractComponentOptions(route.component)
} else {
pageOptions = getPageOptions(route, pages, locales, pagesDir)
pageOptions = getPageOptions(route, pages, locales, pagesDir, defaultLocale)
}

// Skip route if i18n is disabled on page
Expand Down
39 changes: 23 additions & 16 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ exports.getLocaleCodes = getLocaleCodes

/**
* 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
* @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
* @param {String} defaultLocale Default locale from Nuxt's configuration
* @return {Object} Page options
*/
exports.getPageOptions = (route, pages, locales, pagesDir) => {
exports.getPageOptions = (route, pages, locales, pagesDir, defaultLocale) => {
const options = {
locales: getLocaleCodes(locales),
paths: {}
Expand All @@ -47,16 +48,22 @@ exports.getPageOptions = (route, pages, locales, pagesDir) => {
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]
}
})

// Remove disabled locales from page options
options.locales = options.locales.filter(locale => pageOptions[locale] !== false)

// Construct paths object
options.locales
.forEach(locale => {
if (typeof pageOptions[locale] === 'string') {
// Set custom path if any
options.paths[locale] = pageOptions[locale]
} else if (typeof pageOptions[defaultLocale] === 'string') {
// Set default locale's custom path if any
options.paths[locale] = pageOptions[defaultLocale]
}
})

return options
}

Expand Down

0 comments on commit d30e5f0

Please sign in to comment.