Locale switches back to default after `setLocale`
See original GitHub issueVersion
nuxt-i18n: 6.13.12 nuxt: 2.12.2
Nuxt configuration
mode:
- universal
- spa
Nuxt-i18n configuration
i18n: {
lazy: true,
langDir: '../locales/',
locales: ..., // some dynamic code here
strategy: 'prefix',
defaultLocale: 'en',
detectBrowserLanguage: {
// If enabled, a cookie is set once a user has been redirected to his
// preferred language to prevent subsequent redirections
useCookie: true,
// Cookie
cookieKey: 'language',
cookieSecure: true,
// Set to always redirect to value stored in the cookie, not just once
alwaysRedirect: true,
// If no locale for the browsers locale is a match, use this one as a fallback
fallbackLocale: 'en',
},
},
Steps to reproduce
- I am on locale
en
- I select
cs
via$i18n.setLocale('cs');
- cookie is successfuly updated to
cs
- nuxt-auth is redirecting to
/cs
- after next boot something - and I can’t find it in the code (I put debugger to all places in
plugin.main.js
where redirection happens) - is redirecting back to/en
- on visit of
/en
cookie is set back toen
which is expected
What is Expected?
The redirect back to default locale does not happen
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8
Top Results From Across the Web
Locale switches back to default after setLocale #903 - GitHub
When I setLocale that causes a redirect, after redirect the detectBrowserLanguage will prefer the browser language over the value from the ...
Read more >How to reset locale back to original after changing it in Python?
1 Answer 1 ; import locale now = datetime.datetime.now() locale.setlocale(locale.LC_ALL, '') ; "Date using your locale : %s" % now.strftime('%c')) ...
Read more >Locale settings are not right. How can I reset them? - Ask Ubuntu
Using the fallback 'C' locale. The locale command produces error messages locale: Cannot set LC_CTYPE to default locale: No such file or directory...
Read more >locale — Internationalization services — Python 3.11.1 ...
Calling setlocale(LC_ALL, '') lets it use the default locale as defined by the LANG variable. Since we do not want to interfere with...
Read more >Change Locale getting Reset | OutSystems
I have made my application multi lingual. I have different tenants for different countries, each tenant is linked to a default language. When...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@rchl
I did some more debugging and docs reading and the issue is now clear to me.
It’s the combination of
alwaysRedirect
anddetectBrowserLanguage
anduseCookie
.When I
setLocale
that causes a redirect, after redirect thedetectBrowserLanguage
will prefer the browser language over the value from the cookie.I’d expect the browser language would be used only if there’s no cookie but the priority is different. I’m not sure if this is a bug or a feature though.
And then I found out that
onlyOnRoot
mostly fixes the issue for me. But even still, even on/
I’d expect that cookie has higher priority over browser language. Is that just me?So I couldn’t reproduce in a different repo and it made me double check my main app code.
It turned out I had two cookies with same key but with different domains (somehow). I guess it was a leftover from much earlier.
JsCookie.get(cookieKey)
preferred it over the new one and that’s why the language was switching wrongly for me.Sorry for the time waste! I think this can be closed.