Skip to content

Commit

Permalink
feat: Add ignoreRoutes option
Browse files Browse the repository at this point in the history
ignoreRoutes accepts an array of String representing routes that should not be localized
  • Loading branch information
paulgv committed Dec 6, 2017
1 parent 3965f13 commit 56ec3db
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = function (moduleOptions) {
baseRoutes: routes,
locales: options.locales,
defaultLocale: options.defaultLocale,
routesOptions: options.routes
routesOptions: options.routes,
ignoreRoutes: options.ignoreRoutes
})
routes.splice(0, routes.length)
routes.unshift(...newRoutes)
Expand Down
15 changes: 13 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ const { has } = require('lodash')
* defaults to app's configured LOCALES
* @return {Array} Localized routes to be used in Nuxt config
*/
const generateRoutes = ({ baseRoutes, locales, defaultLocale, routesOptions }) => {
const generateRoutes = ({ baseRoutes, locales, defaultLocale, routesOptions, ignoreRoutes }) => {
// Abort routes generation if no routes or locales specified
if (!baseRoutes || !locales) {
return []
}
const newRoutes = []
baseRoutes.forEach((baseRoute) => {
const localizedRoutes = []

// Extract routes that should not be localized
baseRoutes.forEach(route => {
if (ignoreRoutes.indexOf(route.name) !== -1) {
newRoutes.push(route)
} else {
localizedRoutes.push(route)
}
})

localizedRoutes.forEach(baseRoute => {
locales.forEach((locale) => {
const { component } = baseRoute
let { path, name, children } = baseRoute
Expand Down
11 changes: 4 additions & 7 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ module.exports = {
about: {
fr: '/a-propos',
en: '/about-us'
},
category: {
fr: '/categorie'
},
'category-slug': {
fr: '/categorie/:slug'
}
}
},
ignoreRoutes: [
'fr-notlocalized'
]
}]
]
}
10 changes: 10 additions & 0 deletions test/fixture/pages/fr/notlocalized.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<template>
<div>
FR only
</div>
</template>

<script>
export default {
}
</script>
15 changes: 15 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,19 @@ describe('Module', () => {
expect(html).toContain('<a href="/about-us">English</a>')
expect(html).toContain('<a href="/fr/">Accueil</a>')
})

test('/fr/notlocalized contains FR text', async () => {
let html = await get('/fr/notlocalized')
expect(html).toContain('FR only')
})

test('/notlocalized returns 404', async () => {
let response
try {
response = await get('/notlocalized')
} catch (error) {
response = error
}
expect(response.statusCode).toBe(404)
})
})

0 comments on commit 56ec3db

Please sign in to comment.