Skip to content

Commit

Permalink
feat: Add redirectRootToLocale option
Browse files Browse the repository at this point in the history
Provides redirectRootToLocale option to redirect / to a specific locale if noPrefixDefaultLocale is
disabled
  • Loading branch information
paulgv committed Jan 27, 2018
1 parent dac14ae commit 65cacfd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function (moduleOptions) {
defaultLocale: options.defaultLocale,
routesOptions: options.routes,
noPrefixDefaultLocale: options.noPrefixDefaultLocale,
redirectRootToLocale: options.redirectRootToLocale,
ignorePaths: options.ignorePaths
})
routes.splice(0, routes.length)
Expand Down
11 changes: 10 additions & 1 deletion lib/templates/i18n.routing.middleware.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import middleware from './middleware'

middleware['i18n'] = function ({ app, route, error, hotReload }) {
middleware['i18n'] = function ({ app, route, redirect, error, hotReload }) {
const locales = <%= JSON.stringify(options.locales) %>
const localeCodes = locales.map(l => l.code)
const defaultLocale = '<%= options.defaultLocale %>'
// Check if middleware called from hot-reloading, ignore
if (hotReload) return
// Handle / redirection
if (
route.path === '/' &&
'<%= options.redirectRootToLocale %>' &&
!('<%= options.redirectRootToLocale %>' === '<%= options.defaultLocale %>' && <%= options.noPrefixDefaultLocale %> === true) &&
localeCodes.includes('<%= options.redirectRootToLocale %>')) {
redirect('/<%= options.redirectRootToLocale %>/')
}
// Get locale from params
let locale = defaultLocale
locales.forEach(l => {
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module.exports = {
}
],
defaultLocale: 'en',
noPrefixDefaultLocale: true,
redirectRootToLocale: 'en',
vueI18n: {
messages: {
fr: {
Expand Down

0 comments on commit 65cacfd

Please sign in to comment.