Skip to content

Commit

Permalink
Validate AppleLocale against supported locales (#33)
Browse files Browse the repository at this point in the history
Fixes #28
  • Loading branch information
pi0 authored and sindresorhus committed Dec 26, 2018
1 parent 6d95fac commit 2c5f43f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,30 @@ function getLocale(string) {
return (string && string.replace(/[.:].*/, ''));
}

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

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

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

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

function getAppleLocaleSync() {
return execa.sync('defaults', ['read', '-globalDomain', 'AppleLocale']).stdout;
return getSupportedLocale(
execa.sync('defaults', ['read', '-globalDomain', 'AppleLocale']).stdout,
getLocalesSync()
);
}

function getUnixLocale() {
Expand Down

0 comments on commit 2c5f43f

Please sign in to comment.