From d30e5f0f24f8444fabaf8d1e4fb3968f13c4081f Mon Sep 17 00:00:00 2001 From: Jean-Philippe Dos Santos Date: Sat, 13 Jul 2019 22:09:10 +0200 Subject: [PATCH] feat: Use default locale's custom path if not defined for a locale (#354) --- docs/routing.md | 2 ++ src/helpers/routes.js | 2 +- src/helpers/utils.js | 39 +++++++++++++++++++++++---------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/docs/routing.md b/docs/routing.md index c4d4d6e70..174c257b9 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -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+)`. diff --git a/src/helpers/routes.js b/src/helpers/routes.js index c8116a947..ff5c34243 100644 --- a/src/helpers/routes.js +++ b/src/helpers/routes.js @@ -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 diff --git a/src/helpers/utils.js b/src/helpers/utils.js index d9cb3adce..ffbee64ba 100644 --- a/src/helpers/utils.js +++ b/src/helpers/utils.js @@ -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: {} @@ -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 }