2017-05-03 15:35:00 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
const execa = require('execa');
|
|
|
|
const lcid = require('lcid');
|
|
|
|
const mem = require('mem');
|
|
|
|
|
|
|
|
const defaultOpts = {spawn: true};
|
|
|
|
const defaultLocale = 'en_US';
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function getEnvLocale(env) {
|
|
|
|
env = env || process.env;
|
2017-08-14 05:01:11 +02:00
|
|
|
return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseLocale(x) {
|
2017-08-14 05:01:11 +02:00
|
|
|
const env = x.split('\n').reduce((env, def) => {
|
2017-05-03 15:35:00 +02:00
|
|
|
def = def.split('=');
|
2017-08-14 05:01:11 +02:00
|
|
|
env[def[0]] = def[1].replace(/^"|"$/g, '');
|
2017-05-03 15:35:00 +02:00
|
|
|
return env;
|
|
|
|
}, {});
|
|
|
|
return getEnvLocale(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLocale(str) {
|
2017-08-14 05:01:11 +02:00
|
|
|
return (str && str.replace(/[.:].*/, ''));
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function getAppleLocale() {
|
|
|
|
return execa.stdout('defaults', ['read', '-g', 'AppleLocale']);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAppleLocaleSync() {
|
|
|
|
return execa.sync('defaults', ['read', '-g', 'AppleLocale']).stdout;
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function getUnixLocale() {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
return getAppleLocale();
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return execa.stdout('locale')
|
|
|
|
.then(stdout => getLocale(parseLocale(stdout)));
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function getUnixLocaleSync() {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
return getAppleLocaleSync();
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return getLocale(parseLocale(execa.sync('locale').stdout));
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function getWinLocale() {
|
|
|
|
return execa.stdout('wmic', ['os', 'get', 'locale'])
|
|
|
|
.then(stdout => {
|
|
|
|
const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
|
|
|
|
return lcid.from(lcidCode);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
function getWinLocaleSync() {
|
|
|
|
const stdout = execa.sync('wmic', ['os', 'get', 'locale']).stdout;
|
|
|
|
const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
|
|
|
|
return lcid.from(lcidCode);
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
module.exports = mem(opts => {
|
2017-05-03 15:35:00 +02:00
|
|
|
opts = opts || defaultOpts;
|
2017-08-14 05:01:11 +02:00
|
|
|
const envLocale = getEnvLocale();
|
|
|
|
let thenable;
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (envLocale || opts.spawn === false) {
|
|
|
|
thenable = Promise.resolve(getLocale(envLocale));
|
|
|
|
} else if (process.platform === 'win32') {
|
|
|
|
thenable = getWinLocale();
|
|
|
|
} else {
|
|
|
|
thenable = getUnixLocale();
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return thenable.then(locale => locale || defaultLocale)
|
|
|
|
.catch(() => defaultLocale);
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
module.exports.sync = mem(opts => {
|
|
|
|
opts = opts || defaultOpts;
|
|
|
|
const envLocale = getEnvLocale();
|
|
|
|
let res;
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (envLocale || opts.spawn === false) {
|
|
|
|
res = getLocale(envLocale);
|
|
|
|
} else {
|
2017-05-03 15:35:00 +02:00
|
|
|
try {
|
2017-08-14 05:01:11 +02:00
|
|
|
if (process.platform === 'win32') {
|
|
|
|
res = getWinLocaleSync();
|
|
|
|
} else {
|
|
|
|
res = getUnixLocaleSync();
|
|
|
|
}
|
|
|
|
} catch (err) {}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
return res || defaultLocale;
|
|
|
|
});
|