I developed project Nuxt.js in universal mode and localized with i18n
work fine, but now I want to change to in spa mode but i18n doesn’t change the language.
this is code.
file : nuxt.config.js
JavaScript
x
86
86
1
import colors from "vuetify/es5/util/colors";
2
3
export default {
4
mode: "spa",
5
/*
6
** Headers of the page
7
*/
8
head: {
9
meta: [
10
{ charset: "utf-8" },
11
{ name: "viewport", content: "width=device-width, initial-scale=1" },
12
{
13
hid: "description",
14
name: "description",
15
content: process.env.npm_package_description || ""
16
}
17
],
18
link: [
19
{ rel: "icon", type: "image/x-icon", href: "/favicon.ico" },
20
{
21
rel: "stylesheet",
22
href:
23
"https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons"
24
}
25
]
26
},
27
/*
28
** Customize the progress-bar color
29
*/
30
loading: { color: "#fff" },
31
/*
32
** Global CSS
33
*/
34
css: ["~/assets/main.css"],
35
36
router: {
37
middleware: "i18n"
38
},
39
/*
40
** Plugins to load before mounting the App
41
*/
42
plugins: [
43
"~/plugins/mdi-font.js",
44
"~/plugins/i18n.js",
45
"~/plugins/axios.js",
46
{ src: "~/plugins/flag.js", ssr: false }
47
],
48
/*
49
** Nuxt.js modules
50
*/
51
modules: [
52
"@nuxtjs/vuetify",
53
// Doc: https://axios.nuxtjs.org/usage
54
"@nuxtjs/axios"
55
],
56
/*
57
** Axios module configuration
58
** See https://axios.nuxtjs.org/options
59
*/
60
axios: {},
61
/*
62
** vuetify module configuration
63
** https://github.com/nuxt-community/vuetify-module
64
*/
65
vuetify: {
66
theme: {
67
primary: colors.blue.darken2,
68
accent: colors.grey.darken3,
69
secondary: colors.amber.darken3,
70
info: colors.teal.lighten1,
71
warning: colors.amber.base,
72
error: colors.deepOrange.accent4,
73
success: colors.green.accent3
74
}
75
},
76
/*
77
** Build configuration
78
*/
79
build: {
80
/*
81
** You can extend webpack config here
82
*/
83
extend(config, ctx) {}
84
}
85
};
86
file : middleware/i18n.js
JavaScript
1
30
30
1
export default function ({ isHMR, app, store, route, params, req, error, redirect }) {
2
if (isHMR) { // ignore if called from hot module replacement
3
return;
4
}
5
6
if (req) {
7
if (route.name) {
8
let locale = null;
9
10
// check if the locale cookie is set
11
if (req.headers.cookie) {
12
const cookies = req.headers.cookie.split('; ').map(stringCookie => stringCookie.split('='));
13
const cookie = cookies.find(cookie => cookie[0] === 'locale');
14
15
if (cookie) {
16
locale = cookie[1];
17
}
18
}
19
20
// if the locale cookie is not set, fallback to accept-language header
21
if (!locale) {
22
locale = req.headers['accept-language'].split(',')[0].toLocaleLowerCase().substring(0, 2);
23
}
24
25
store.commit('SET_LANG', locale);
26
app.i18n.locale = store.state.locale;
27
}
28
}
29
};
30
store/index.js
JavaScript
1
29
29
1
export const state = () => ({
2
locales: [
3
{
4
code: 'en',
5
name: 'EN',
6
flag: 'us'
7
},
8
{
9
code: 'fa',
10
name: 'FA',
11
flag: 'af'
12
},
13
{
14
code: 'pa',
15
name: 'PA',
16
flag: 'af'
17
}
18
],
19
locale: 'en'
20
});
21
22
export const mutations = {
23
SET_LANG(state, locale) {
24
if (state.locales.find(el => el.code === locale)) {
25
state.locale = locale
26
}
27
}
28
};
29
and licalize file are in locales/fa.json and en.json
Advertisement
Answer
export default function ({ isHMR, app, store, route, params, req, error, redirect })
So bold parameter req is not receive in SAP mode. For more information you can visit this link.
So you have to try another way using another parameter, Just for example you can see below code:
JavaScript
1
31
31
1
export default function ({
2
isHMR, app, store, route, params, error, redirect
3
}) {
4
const defaultLocale = app.i18n.fallbackLocale
5
// If middleware is called from hot module replacement, ignore it
6
if (isHMR) return
7
// Get locale from params
8
9
const locale = params.lang || defaultLocale
10
if (store.state.locales.indexOf(locale) === -1) {
11
return error({
12
message: 'This page could not be found.',
13
statusCode: 404
14
})
15
}
16
17
// Set locale
18
store.commit('SET_LANG', locale)
19
app.i18n.locale = store.state.locale
20
// If route is /<defaultLocale>/... -> redirect to /...
21
22
if (locale === defaultLocale && route.fullPath.indexOf('/' + defaultLocale) ===
23
0) {
24
const toReplace = '^/' + defaultLocale
25
const re = new RegExp(toReplace)
26
return redirect(
27
route.fullPath.replace(re, '/')
28
)
29
}
30
}
31