-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathos-locale.js
More file actions
158 lines (125 loc) · 3.69 KB
/
os-locale.js
File metadata and controls
158 lines (125 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
/**
* ******************************************************
* @author https://github.com/sindresorhus/os-locale *
* @description reference source code for compatibility *
* ******************************************************
*/
const lcid = require('lcid');
const { exec, execSync } = require('./exec.js');
const defaultOptions = { spawn: true };
const defaultLocale = 'en-US';
async function getStdOut(command, args) {
return (await exec(command, args)).stdout;
}
function getStdOutSync(command, args) {
return execSync(command, args);
}
function getEnvLocale(env = process.env) {
return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
}
function parseLocale(string) {
const env = {};
for (const definition of string.split('\n')) {
const [key, value] = definition.split('=');
env[key] = value.replace(/^"|"$/g, '');
}
return getEnvLocale(env);
}
function getLocale(string) {
return (string && string.replace(/[.:].*/, ''));
}
async function getLocales() {
return getStdOut('locale', ['-a']);
}
function getLocalesSync() {
return getStdOutSync('locale', ['-a']);
}
function getSupportedLocale(locale, locales = '') {
return locales.includes(locale) ? locale : defaultLocale;
}
async function getAppleLocale() {
const results = await Promise.all([
getStdOut('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocales(),
]);
return getSupportedLocale(results[0], results[1]);
}
function getAppleLocaleSync() {
return getSupportedLocale(
getStdOutSync('defaults', ['read', '-globalDomain', 'AppleLocale']),
getLocalesSync(),
);
}
async function getUnixLocale() {
return getLocale(parseLocale(await getStdOut('locale')));
}
function getUnixLocaleSync() {
return getLocale(parseLocale(getStdOutSync('locale')));
}
async function getWinLocale() {
const stdout = await getStdOut('wmic', ['os', 'get', 'locale']);
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
}
function getWinLocaleSync() {
const stdout = getStdOutSync('wmic', ['os', 'get', 'locale']);
const lcidCode = Number.parseInt(stdout.replace('Locale', ''), 16);
return lcid.from(lcidCode);
}
function normalise(input) {
return input.replace(/_/, '-');
}
const cache = new Map();
async function osLocale(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
let locale;
try {
const envLocale = getEnvLocale();
if (envLocale || options.spawn === false) {
locale = getLocale(envLocale);
} else if (process.platform === 'win32') {
locale = await getWinLocale();
} else if (process.platform === 'darwin') {
locale = await getAppleLocale();
} else {
locale = await getUnixLocale();
}
// eslint-disable-next-line no-unused-vars
} catch (e) {
// ignore
}
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
}
function osLocaleSync(options = defaultOptions) {
if (cache.has(options.spawn)) {
return cache.get(options.spawn);
}
let locale;
try {
const envLocale = getEnvLocale();
if (envLocale || options.spawn === false) {
locale = getLocale(envLocale);
} else if (process.platform === 'win32') {
locale = getWinLocaleSync();
} else if (process.platform === 'darwin') {
locale = getAppleLocaleSync();
} else {
locale = getUnixLocaleSync();
}
// eslint-disable-next-line no-unused-vars
} catch (e) {
// ignore
}
const normalised = normalise(locale || defaultLocale);
cache.set(options.spawn, normalised);
return normalised;
}
module.exports = {
osLocale,
osLocaleSync
};