Projekt

Obecné

Profil

Stáhnout (2.64 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2
const execa = require('execa');
3
const lcid = require('lcid');
4
const mem = require('mem');
5

    
6
const defaultOptions = {spawn: true};
7
const defaultLocale = 'en_US';
8

    
9
function getEnvLocale(env = process.env) {
10
	return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
11
}
12

    
13
function parseLocale(string) {
14
	const env = string.split('\n').reduce((env, def) => {
15
		const [key, value] = def.split('=');
16
		env[key] = value.replace(/^"|"$/g, '');
17
		return env;
18
	}, {});
19

    
20
	return getEnvLocale(env);
21
}
22

    
23
function getLocale(string) {
24
	return (string && string.replace(/[.:].*/, ''));
25
}
26

    
27
function getLocales() {
28
	return execa.stdout('locale', ['-a']);
29
}
30

    
31
function getLocalesSync() {
32
	return execa.sync('locale', ['-a']).stdout;
33
}
34

    
35
function getSupportedLocale(locale, locales = '') {
36
	return locales.includes(locale) ? locale : defaultLocale;
37
}
38

    
39
function getAppleLocale() {
40
	return Promise.all([
41
		execa.stdout('defaults', ['read', '-globalDomain', 'AppleLocale']),
42
		getLocales()
43
	]).then(results => getSupportedLocale(results[0], results[1]));
44
}
45

    
46
function getAppleLocaleSync() {
47
	return getSupportedLocale(
48
		execa.sync('defaults', ['read', '-globalDomain', 'AppleLocale']).stdout,
49
		getLocalesSync()
50
	);
51
}
52

    
53
function getUnixLocale() {
54
	if (process.platform === 'darwin') {
55
		return getAppleLocale();
56
	}
57

    
58
	return execa.stdout('locale')
59
		.then(stdout => getLocale(parseLocale(stdout)));
60
}
61

    
62
function getUnixLocaleSync() {
63
	if (process.platform === 'darwin') {
64
		return getAppleLocaleSync();
65
	}
66

    
67
	return getLocale(parseLocale(execa.sync('locale').stdout));
68
}
69

    
70
function getWinLocale() {
71
	return execa.stdout('wmic', ['os', 'get', 'locale'])
72
		.then(stdout => {
73
			const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
74
			return lcid.from(lcidCode);
75
		});
76
}
77

    
78
function getWinLocaleSync() {
79
	const {stdout} = execa.sync('wmic', ['os', 'get', 'locale']);
80
	const lcidCode = parseInt(stdout.replace('Locale', ''), 16);
81
	return lcid.from(lcidCode);
82
}
83

    
84
module.exports = mem((options = defaultOptions) => {
85
	const envLocale = getEnvLocale();
86

    
87
	let thenable;
88
	if (envLocale || options.spawn === false) {
89
		thenable = Promise.resolve(getLocale(envLocale));
90
	} else if (process.platform === 'win32') {
91
		thenable = getWinLocale();
92
	} else {
93
		thenable = getUnixLocale();
94
	}
95

    
96
	return thenable
97
		.then(locale => locale || defaultLocale)
98
		.catch(() => defaultLocale);
99
});
100

    
101
module.exports.sync = mem((options = defaultOptions) => {
102
	const envLocale = getEnvLocale();
103

    
104
	let res;
105
	if (envLocale || options.spawn === false) {
106
		res = getLocale(envLocale);
107
	} else {
108
		try {
109
			res = process.platform === 'win32' ? getWinLocaleSync() : getUnixLocaleSync();
110
		} catch (_) {}
111
	}
112

    
113
	return res || defaultLocale;
114
});
(1-1/4)