Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node JS - how to detect user language

Tags:

node.js

I'm trying to check user language in pure node js, without any extensions from npm. I used process and global command, there is a lot of specifications but i couldn't find system language

like image 518
Szach Avatar asked Sep 06 '17 09:09

Szach


People also ask

How do I get Node.js language?

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

What language does node use?

The Node. js runtime is built on top of a programming language—in this case, JavaScript—and helps in running frameworks itself.

Is Node.js code visible?

No, it is not visible to the browser. The language is JavaScript but Node totally server-side. it's as secure as any other server environment "Node".

How can I get browser language?

By using JavaScript, this task can be easily done by using: Languages property is available for the navigator interface, which returns the most preferred / user preferred language set in the web browser. This property is read-only.


2 Answers

Here is an OS/node/browser independent way of obtaining the default locale

let locale = Intl.DateTimeFormat().resolvedOptions().locale;
console.log(locale);
like image 110
Jacott Avatar answered Nov 07 '22 08:11

Jacott


Answer is in your question. You don't want to use a plugin, but you can take a look on how they do it :)

The answer is the environment

Spoiler

function getEnvLocale(env) {
    env = env || process.env;

    return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
}

Don't forget to adapt to the different platform (mac, linux, windows...)

like image 7
Orelsanpls Avatar answered Nov 07 '22 07:11

Orelsanpls