Skip to content

Commit 2a3fccb

Browse files
committed
Switch from underscore to dash for the locale: en_USen-US
Fixes #30
1 parent 4c2caff commit 2a3fccb

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ declare const osLocale: {
2121
2222
(async () => {
2323
console.log(await osLocale());
24-
//=> 'en_US'
24+
//=> 'en-US'
2525
})();
2626
```
2727
*/

index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const lcid = require('lcid');
44
const mem = require('mem');
55

66
const defaultOptions = {spawn: true};
7-
const defaultLocale = 'en_US';
7+
const defaultLocale = 'en-US';
88

99
function getEnvLocale(env = process.env) {
1010
return env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE;
@@ -24,7 +24,7 @@ function getLocale(string) {
2424
return (string && string.replace(/[.:].*/, ''));
2525
}
2626

27-
function getLocales() {
27+
async function getLocales() {
2828
return execa.stdout('locale', ['-a']);
2929
}
3030

@@ -81,37 +81,37 @@ function getWinLocaleSync() {
8181
}
8282

8383
const osLocale = mem(async (options = defaultOptions) => {
84-
const envLocale = getEnvLocale();
85-
84+
let locale;
8685
try {
87-
let locale;
86+
const envLocale = getEnvLocale();
87+
8888
if (envLocale || options.spawn === false) {
8989
locale = getLocale(envLocale);
9090
} else if (process.platform === 'win32') {
9191
locale = await getWinLocale();
9292
} else {
9393
locale = await getUnixLocale();
9494
}
95+
} catch (_) {}
9596

96-
return locale || defaultLocale;
97-
} catch (_) {
98-
return defaultLocale;
99-
}
97+
return (locale || defaultLocale).replace(/_/, '-');
10098
});
10199

102100
module.exports = osLocale;
103101

104102
module.exports.sync = mem((options = defaultOptions) => {
105-
const envLocale = getEnvLocale();
106-
107-
let result;
108-
if (envLocale || options.spawn === false) {
109-
result = getLocale(envLocale);
110-
} else {
111-
try {
112-
result = process.platform === 'win32' ? getWinLocaleSync() : getUnixLocaleSync();
113-
} catch (_) {}
114-
}
103+
let locale;
104+
try {
105+
const envLocale = getEnvLocale();
106+
107+
if (envLocale || options.spawn === false) {
108+
locale = getLocale(envLocale);
109+
} else if (process.platform === 'win32') {
110+
locale = getWinLocaleSync();
111+
} else {
112+
locale = getUnixLocaleSync();
113+
}
114+
} catch (_) {}
115115

116-
return result || defaultLocale;
116+
return (locale || defaultLocale).replace(/_/, '-');
117117
});

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const osLocale = require('os-locale');
3434

3535
(async () => {
3636
console.log(await osLocale());
37-
//=> 'en_US'
37+
//=> 'en-US'
3838
})();
3939
```
4040

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import test from 'ava';
33
import importFresh from 'import-fresh';
44

55
const envVars = ['LC_ALL', 'LANGUAGE', 'LANG', 'LC_MESSAGES'];
6-
const expectedFallback = 'en_US';
6+
const expectedFallback = 'en-US';
77

88
function unsetEnvVars(cache) {
99
for (const envVar of envVars) {
@@ -26,14 +26,14 @@ test('async', async t => {
2626
const locale = await importFresh('.')();
2727
console.log('Locale identifier:', locale);
2828
t.true(locale.length > 1);
29-
t.true(locale.includes('_'));
29+
t.true(locale.includes('-'));
3030
});
3131

3232
test('sync', t => {
3333
const locale = importFresh('.').sync();
3434
console.log('Locale identifier:', locale);
3535
t.true(locale.length > 1);
36-
t.true(locale.includes('_'));
36+
t.true(locale.includes('-'));
3737
});
3838

3939
test('async without spawn', async t => {

0 commit comments

Comments
 (0)