question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Locale switches back to default after `setLocale`

See original GitHub issue

Version

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

  1. I am on locale en
  2. I select cs via $i18n.setLocale('cs');
  3. cookie is successfuly updated to cs
  4. nuxt-auth is redirecting to /cs
  5. 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
  6. on visit of /en cookie is set back to en which is expected

What is Expected?

The redirect back to default locale does not happen

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

1reaction
MartinMalindacommented, Sep 24, 2020

@rchl

I did some more debugging and docs reading and the issue is now clear to me.

It’s the combination of alwaysRedirect and detectBrowserLanguage and useCookie.

When I setLocale that causes a redirect, after redirect the detectBrowserLanguage 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?

0reactions
MartinMalindacommented, Sep 24, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found