Nuxt/examples/i18n/middleware/i18n.js

17 lines
633 B
JavaScript
Raw Normal View History

2017-05-31 11:21:55 +00:00
export default function ({ app, store, route, params, error, redirect, hotReload }) {
2017-07-08 17:55:39 +00:00
// If middleware is called from hot-reloading, ignore it
2017-05-31 11:21:55 +00:00
if (hotReload) return
// Get locale from params
2017-04-14 09:55:04 +00:00
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)
2017-05-21 19:01:08 +00:00
app.i18n.locale = store.state.locale
2017-04-14 09:55:04 +00:00
// If route is /en/... -> redirect to /...
if (locale === 'en' && route.fullPath.indexOf('/en') === 0) {
2017-05-31 11:21:55 +00:00
return redirect(route.fullPath.replace(/^\/en/, '/'))
2017-02-08 17:49:16 +00:00
}
}