Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 614
Oleg Dats Avatar asked Oct 11 '22 07:10

Oleg Dats


People also ask

What language does Express js use?

Express is a popular unopinionated web framework, written in JavaScript and hosted within the Node. js runtime environment. This module explains some of the key benefits of the framework, how to set up your development environment and how to perform common web development and deployment tasks.

Can Node.js be used in a browser?

js is a server-side JavaScript run-time environment. It's open-source, including Google's V8 engine, libuv for cross-platform compatibility, and a core library. Notably, Node. js does not expose a global "window" object, since it does not run within a browser.

On which browser engine is Node.js built?

We already discussed the first line of this definition: “Node. js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.” Now let's understand the other two lines so we can find out why Node. js is so popular.


2 Answers

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.

like image 100
Joachim Isaksson Avatar answered Oct 12 '22 21:10

Joachim Isaksson


request.acceptsLanguages will contain a parsed version of request.headers['accept-language'].

See: http://expressjs.com/en/api.html#req.acceptsLanguages

like image 39
cGuille Avatar answered Oct 12 '22 21:10

cGuille