Route switching when using i18n setLocale doesn't work...?
Hey there Nuxt community!
I've been having a problem with my Nuxt application, (seemingly after upgrading my Nuxt package from 3.11.1 to 3.11.2 - but I'm not sure if that's the sole reason....)
My problem is that when I switch the locale for my webapp, I don't have a "re-route" to the new route (for example from english ("/") to german ("/de-DE"), it stays on "/").
My current code looks something like this (like I said, after various iterations of things that didn't appear to work)
I'm pretty sure that switchPath version used to work before I upgraded my Nuxt version.... But currently it doesn't anymore. The cookie value does get changed, but the site url doesn't, which sucks, cause when refreshing the page it will swap back to the original language (not the new one that it was supposed to use)...
I'd really appreciate it if anyone could give me some pointers/things to try or knows any other tips! Thanks!
16 Replies
I checked through some of these too:
https://i18n.nuxtjs.org/docs/guide/lang-switcher
https://v8.i18n.nuxtjs.org/guide/lang-switcher
https://v8.i18n.nuxtjs.org/api/composables#uselocaleroute
Lang Switcher
When Nuxt i18n module is loaded in your app, it adds your locales configuration to nuxtApp.$i18n (or this.$i18n), which makes it really easy to display a lang switcher anywhere in your app.
Composables
Composition API for Nuxt i18n module.
After some more random testing, this here seems to work, but i'm just still stumped, why the other cases don't really seem to do it for me...?
Try setting the
locale.value
and calling setLocale()
I think I had a similar issue and when I used locale.value = 'en'
it would work but not "persist", setLocale
seems to do the persisting to cookie part if I remember correctly from the docsYeah I'm pretty sure setLocale(code) was in fact setting the cookie -> but it wasn't changing the route params (the prefixes)
Does setting the
locale
var from i18n help in your case?You mean with this?
Yes, that code works for me to ensure the locale is set and "persisted"
My nuxt config (i18n) by the way, in case it helps:
Yeah that's what I was currently trying
It does change the locale (like I mentioned), which is cool, but it doesn't also change the app's route
Oh I didn't see those lines
npnp
And yeah, this version does seem to work, I'm just confused why the others (with switchLocalePath and all) didn't :d
Yeah, I can understand why. Even though the https://i18n.nuxtjs.org/docs/guide/lang-switcher#vue-i18n-caveat doesn't recommend it, I didn't get it to work another way in my app though I don't use routing prefixes in my case.
Thanks for the help though! Appreciated
Maybe someone else can chime in that knows more about vue-i18n/ NUXT i18n module, that can help with your issue and I'll maybe get smarter about not using
locale.value
anymore for my case 😄Yeah I have a lot to learn with it too :D