Socket
Socket
Sign inDemoInstall

os-locale

Package Overview
Dependencies
21
Maintainers
3
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.4.0 to 2.0.0

158

index.js
'use strict';
var childProcess = require('child_process');
var execFileSync = childProcess.execFileSync;
var lcid = require('lcid');
var defaultOpts = {spawn: true};
var cache;
const execa = require('execa');
const lcid = require('lcid');
const mem = require('mem');
function fallback() {
cache = 'en_US';
return cache;
}
const defaultOpts = {spawn: true};
const defaultLocale = 'en_US';
function getEnvLocale(env) {
env = env || process.env;
var ret = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
cache = getLocale(ret);
return ret;
return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
}
function parseLocale(x) {
var env = x.split('\n').reduce(function (env, def) {
const env = x.split('\n').reduce((env, def) => {
def = def.split('=');
env[def[0]] = def[1];
env[def[0]] = def[1].replace(/^"|"$/g, '');
return env;

@@ -30,99 +24,79 @@ }, {});

function getLocale(str) {
return (str && str.replace(/[.:].*/, '')) || fallback();
return (str && str.replace(/[.:].*/, ''));
}
module.exports = function (opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = defaultOpts;
} else {
opts = opts || defaultOpts;
}
function getAppleLocale() {
return execa.stdout('defaults', ['read', '-g', 'AppleLocale']);
}
if (cache || getEnvLocale() || opts.spawn === false) {
setImmediate(cb, null, cache);
return;
function getAppleLocaleSync() {
return execa.sync('defaults', ['read', '-g', 'AppleLocale']).stdout;
}
function getUnixLocale() {
if (process.platform === 'darwin') {
return getAppleLocale();
}
var getAppleLocale = function () {
childProcess.execFile('defaults', ['read', '-g', 'AppleLocale'], function (err, stdout) {
if (err) {
fallback();
return;
}
return execa.stdout('locale')
.then(stdout => getLocale(parseLocale(stdout)));
}
cache = stdout.trim() || fallback();
cb(null, cache);
});
};
function getUnixLocaleSync() {
if (process.platform === 'darwin') {
return getAppleLocaleSync();
}
if (process.platform === 'win32') {
childProcess.execFile('wmic', ['os', 'get', 'locale'], function (err, stdout) {
if (err) {
fallback();
return;
}
return getLocale(parseLocale(execa.sync('locale').stdout));
}
var lcidCode = parseInt(stdout.replace('Locale', ''), 16);
cache = lcid.from(lcidCode) || fallback();
cb(null, cache);
function getWinLocale() {
return execa.stdout('wmic', ['os', 'get', 'locale'])
.then(stdout => {
const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
});
} else {
childProcess.execFile('locale', function (err, stdout) {
if (err) {
fallback();
return;
}
}
var res = parseLocale(stdout);
function getWinLocaleSync() {
const stdout = execa.sync('wmic', ['os', 'get', 'locale']).stdout;
const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
}
if (!res && process.platform === 'darwin') {
getAppleLocale();
return;
}
cache = getLocale(res);
cb(null, cache);
});
}
};
module.exports.sync = function (opts) {
module.exports = mem(opts => {
opts = opts || defaultOpts;
const envLocale = getEnvLocale();
let thenable;
if (cache || getEnvLocale() || !execFileSync || opts.spawn === false) {
return cache;
if (envLocale || opts.spawn === false) {
thenable = Promise.resolve(getLocale(envLocale));
} else if (process.platform === 'win32') {
thenable = getWinLocale();
} else {
thenable = getUnixLocale();
}
if (process.platform === 'win32') {
var stdout;
return thenable.then(locale => locale || defaultLocale)
.catch(() => defaultLocale);
});
try {
stdout = execFileSync('wmic', ['os', 'get', 'locale'], {encoding: 'utf8'});
} catch (err) {
return fallback();
}
module.exports.sync = mem(opts => {
opts = opts || defaultOpts;
const envLocale = getEnvLocale();
let res;
var lcidCode = parseInt(stdout.replace('Locale', ''), 16);
cache = lcid.from(lcidCode) || fallback();
return cache;
}
var res;
try {
res = parseLocale(execFileSync('locale', {encoding: 'utf8'}));
} catch (err) {}
if (!res && process.platform === 'darwin') {
if (envLocale || opts.spawn === false) {
res = getLocale(envLocale);
} else {
try {
cache = execFileSync('defaults', ['read', '-g', 'AppleLocale'], {encoding: 'utf8'}).trim() || fallback();
return cache;
} catch (err) {
return fallback();
}
if (process.platform === 'win32') {
res = getWinLocaleSync();
} else {
res = getUnixLocaleSync();
}
} catch (err) {}
}
cache = getLocale(res);
return cache;
};
return res || defaultLocale;
});
{
"name": "os-locale",
"version": "1.4.0",
"version": "2.0.0",
"description": "Get the system locale",

@@ -13,3 +13,3 @@ "license": "MIT",

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},

@@ -37,3 +37,5 @@ "scripts": {

"dependencies": {
"lcid": "^1.0.0"
"execa": "^0.5.0",
"lcid": "^1.0.0",
"mem": "^1.1.0"
},

@@ -44,3 +46,6 @@ "devDependencies": {

"xo": "*"
},
"xo": {
"esnext": true
}
}
# os-locale [![Build Status](https://travis-ci.org/sindresorhus/os-locale.svg?branch=master)](https://travis-ci.org/sindresorhus/os-locale)
> Get the system [locale](http://en.wikipedia.org/wiki/Locale)
> Get the system [locale](https://en.wikipedia.org/wiki/Locale_(computer_software))

@@ -20,5 +20,5 @@ Useful for localizing your module or app.

```js
var osLocale = require('os-locale');
const osLocale = require('os-locale');
osLocale(function (err, locale) {
osLocale.then(locale => {
console.log(locale);

@@ -32,4 +32,6 @@ //=> 'en_US'

### osLocale([options], callback(error, locale))
### osLocale([options])
Returns a `Promise` for the locale.
### osLocale.sync([options])

@@ -39,5 +41,9 @@

#### options.spawn
#### options
Type: `boolean`
Type: `Object`
##### spawn
Type: `boolean`<br>
Default: `true`

@@ -50,2 +56,2 @@

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc