JuanPablo
JuanPablo

Reputation: 24774

nodejs, get locale of OS

in python with getdefaultlocale I can get the locale

>>> import locale
>>> locale.getdefaultlocale()
('es_ES', 'UTF-8')

in nodejs exists some similar?

Upvotes: 3

Views: 3569

Answers (2)

Julian Knight
Julian Knight

Reputation: 4923

Here is a simple method.

const locale = (process.env.LANG || process.env.LANGUAGE || process.env.LC_ALL || process.env.LC_MESSAGES).split('.')[0]

console.log(locale)

Which should work on Windows and Linux, hopefully Mac as well though I didn't test that.

Note that if doing this on 1 line as illustrated, you might want to wrap it in a try/catch just in case you get an env response that doesn't have a "." in it.

Upvotes: 0

Bill_BsB
Bill_BsB

Reputation: 314

Unfortunately, it's not as straightforward as it seems. The docs tells the whole story. There's also os-locale that it might be helpful.

But if you happen to be running a simulated browser environment, you can try this: console.log('navigator.language:', navigator.language);

Upvotes: 1

Related Questions