Nuxt/examples/i18n/middleware/i18n.js
Sébastien Chopin 6f84f69b7a Fix paths & typo
2017-07-08 19:55:39 +02:00

17 lines
633 B
JavaScript

export default function ({ app, store, route, params, error, redirect, hotReload }) {
// If middleware is called from hot-reloading, ignore it
if (hotReload) return
// Get locale from params
const locale = params.lang || 'en'
if (store.state.locales.indexOf(locale) === -1) {
return error({ message: 'This page could not be found.', statusCode: 404 })
}
// Set locale
store.commit('SET_LANG', locale)
app.i18n.locale = store.state.locale
// If route is /en/... -> redirect to /...
if (locale === 'en' && route.fullPath.indexOf('/en') === 0) {
return redirect(route.fullPath.replace(/^\/en/, '/'))
}
}