Detect browser language
See original GitHub issueFirst 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:
- Created 6 years ago
- Reactions:6
- Comments:13 (4 by maintainers)
Top 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 >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
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.
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.