2017-08-23 11:41:25 +00:00
|
|
|
export default function ({ isHMR, app, store, route, params, error, redirect }) {
|
2018-03-01 13:03:36 +00:00
|
|
|
const defaultLocale = app.i18n.fallbackLocale
|
2017-08-23 11:41:25 +00:00
|
|
|
// If middleware is called from hot module replacement, ignore it
|
2019-07-10 10:45:49 +00:00
|
|
|
if (isHMR) { return }
|
2017-05-31 11:21:55 +00:00
|
|
|
// Get locale from params
|
2018-02-28 22:51:58 +00:00
|
|
|
const locale = params.lang || defaultLocale
|
2019-07-10 10:45:49 +00:00
|
|
|
if (!store.state.locales.includes(locale)) {
|
2017-04-14 09:55:04 +00:00
|
|
|
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
|
2018-02-28 22:51:58 +00:00
|
|
|
// If route is /<defaultLocale>/... -> redirect to /...
|
2018-03-01 13:03:36 +00:00
|
|
|
if (locale === defaultLocale && route.fullPath.indexOf('/' + defaultLocale) === 0) {
|
2018-06-27 06:14:04 +00:00
|
|
|
const toReplace = '^/' + defaultLocale + (route.fullPath.indexOf('/' + defaultLocale + '/') === 0 ? '/' : '')
|
2018-03-01 13:03:36 +00:00
|
|
|
const re = new RegExp(toReplace)
|
2018-02-28 22:51:58 +00:00
|
|
|
return redirect(
|
|
|
|
route.fullPath.replace(re, '/')
|
|
|
|
)
|
2017-02-08 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|