How can I get the browser language in node.js (express.js)?


User requests some page and I want to know (on server side) what is the language in his/her browser. So I could render template with the right messages.

On client side it's easy:

var language = window.navigator.userLanguage || window.navigator.language
4/26/2017 5:47:13 PM

Accepted Answer

You can use req.headers["accept-language"] to get the language/locale the user has set in his browser.

For easier support, you may want to look into a locale module.

8/7/2012 12:05:38 PM

With Express 4.x, you can use the build in req.acceptsLanguages(lang [, ...]) to check if certain languages are accepted.

var express = require('express');
app.get('/translation', function(request, response) {
    var lang = request.acceptsLanguages('fr', 'es', 'en');
    if (lang) {
        console.log('The first accepted of [fr, es, en] is: ' + lang);
    } else {
        console.log('None of [fr, es, en] is accepted');

To get the list of all accepted languages, using Express 4.x, you can use the module accepts.

var express = require('express'), accepts = require('accepts');
app.get('/translation', function(request, response) {

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow