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.

Detect browser language

See original GitHub issue

First of all, thanks for this module, is pretty useful! 👍

Now, it would be great if the module is able to detect the browser language and redirect to the user accordingly, so for example, if my browser has the english language enabled, I’d be redirected to '/en'.

I wrote my own middleware for that:

export default function ({ isHMR, app, store, route, params, req, error, redirect }) {
  if (isHMR) return

  if (req) {
    if (route.name) {
      let locale = req.headers['accept-language'].split(',')[0].toLocaleLowerCase().substring(0, 2)

      if (locale === 'en') { // In my case I just care about English or German but of course that line should be a condition to detect if the locale is part of the locales array in the configuration
        app.i18n.locale = locale

        if (params && Object.keys(params).length === 0 && params.constructor === Object) {
          redirect(app.localePath(app.getRouteBaseName()))
        } else {
          redirect(app.localePath({name: app.getRouteBaseName(), params: params}))
        }
      }
    }
  }
}

But I think it would be better if this can be done by the module itself, maybe as an option in the configuration, something like detectBrowserLanguage: true

What do you think?

<div align="right">This question is available on Nuxt.js community (#c32)</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
amrshakyacommented, Sep 7, 2018

detectBrowserLanguage: { useCookie: true, cookieKey: ‘i18n_lang_cookie’ },

i did set detectBrowserLanguage but it didn’t work as it should. like if i select german language i would expect site to be loaded in german language but, it still loads in english.

5reactions
paulgvcommented, Mar 10, 2018

Awesome! Thank you @eddiesigner! I’ve added this feature in v2.5.0, it’s pretty much all your code with the addition of some cookie handling to prevent redirecting users every time they load a page if they chose another language.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript for detecting browser language preference [duplicate]
I think the main problem here is that the browser settings don't actually affect the navigator.language property that is obtained via javascript.
Read more >
Detecting Browser Language Preference with JavaScript
Let's find out how to detect browser language preference with JavaScript to switch to the most preferred locale available for a user.
Read more >
How to detect the browser language preference using ...
How to detect the browser language preference using JavaScript ? · The navigator.languages property will return an array which stores the ...
Read more >
Navigator.language - Web APIs | MDN
The Navigator.language read-only property returns a string representing the preferred language of the user, usually the language of the ...
Read more >
display the list of languages your browser says you prefer
Changing your browser locale or language will cause some web pages to be displayed in a different language. This tool shows you the...
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