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.

Set cookie to locale that it redirected to, and always redirect (unless manually switched to different locale)

See original GitHub issue

What problem does this feature solve?

I implemented my own version of detectBrowserLanguage when I was still using an older version of nuxt-i18n and I was happy to see this feature was added in a later version! I just tried it out, and I think the current implementation can be improved, because now it will only redirect the first time, not when a user returns to the main url.

What does the proposed changes look like?

The cookie should be set to the initially detected language, en then always check what the (last selected) language is set to in the cookie. Then we can always redirect to the language stored in the cookie. If the user manually switched language through the switchLocalePath function, the cookie can be updated with the manually selected language, and also remember this manual overwrite on the next visit.

<div align="right">This feature request is available on Nuxt.js community (#c81)</div>

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:16

github_iconTop GitHub Comments

5reactions
borie88commented, Mar 3, 2019

Thanks. I ended up doing something similar, making a plugin:

import VueCookie from 'vue-cookie'

export default function ({ app }) {
  app.i18n.onLanguageSwitched = (oldLocale, newLocale) => {
    if (process.browser) {
      if (VueCookie.get('i18n_redirected') !== newLocale) {
        VueCookie.set('i18n_redirected', newLocale)
      }
    }
  }
}
3reactions
rchlcommented, May 31, 2019

@stact despite the naming, switchLocalePath just returns URL of current page for given locale. So it wouldn’t really fit to make it set the cookie. Remember it can be used multiple time in a template and then it would change state every time it’s called.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set cookie to locale that it redirected to, and always ... - GitHub
Then we can always redirect to the language stored in the cookie. If the user manually switched language through the switchLocalePath function, ...
Read more >
How to force Next.js to always redirect to a preferred user ...
By default, Next.js automatically detects the user's preferred locale based on the Accept-Language header sent in the page request.
Read more >
Localization Redirects with cookies via Variables Support!
A discussion with one of my customers led me to explore if their full localization redirection logic could be moved into the property ......
Read more >
Set-Cookie - HTTP - MDN Web Docs
Indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost), and...
Read more >
next.config.js: Redirects
Add redirects to your Next.js app. ... Header, Cookie, and Query Matching ... is set source: '/nl/with-locale-manual', destination: '/nl/another', locale: ...
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