Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Inline code to detect current locale from environment #1356

Merged
merged 1 commit into from Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,6 @@
"cliui": "^5.0.0",
"find-up": "^3.0.0",
"get-caller-file": "^2.0.1",
"os-locale": "^3.1.0",
"require-directory": "^2.1.1",
"require-main-filename": "^2.0.0",
"set-blocking": "^2.0.0",
Expand Down
1 change: 0 additions & 1 deletion test/validation.js
Expand Up @@ -291,7 +291,6 @@ describe('validation tests', () => {

function loadLocale (locale) {
delete require.cache[require.resolve('../')]
delete require.cache[require.resolve('os-locale')]
yargs = require('../')
process.env.LC_ALL = locale
}
Expand Down
12 changes: 0 additions & 12 deletions test/yargs.js
Expand Up @@ -638,7 +638,6 @@ describe('yargs dsl tests', () => {

function loadLocale (locale) {
delete require.cache[require.resolve('../')]
delete require.cache[require.resolve('os-locale')]
yargs = require('../')
process.env.LC_ALL = locale
}
Expand Down Expand Up @@ -687,17 +686,6 @@ describe('yargs dsl tests', () => {
r.logs.join(' ').should.match(/Exibe ajuda/)
})

it('handles os-locale throwing an exception', () => {
// make os-locale throw.
require('os-locale')
require.cache[require.resolve('os-locale')].exports.sync = () => { throw Error('an error!') }

delete require.cache[require.resolve('../')]
yargs = require('../')

yargs.locale().should.equal('en')
})

it('uses locale string for help option default desc on .locale().help()', () => {
const r = checkOutput(() => {
yargs(['-h'])
Expand Down
5 changes: 3 additions & 2 deletions yargs.js
Expand Up @@ -1181,8 +1181,9 @@ function Yargs (processArgs, cwd, parentRequire) {
if (!detectLocale) return

try {
const osLocale = require('os-locale')
self.locale(osLocale.sync({ spawn: false }))
const { env } = process
const locale = env.LC_ALL || env.LC_MESSAGES || env.LANG || env.LANGUAGE || 'en_US'
self.locale(locale.replace(/[.:].*/, ''))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory we the try / catch block is no longer needed but the way os-locale was used I think it was impossible for it to throw, therefore I assume that it's possible for self.locale() to cause an exception.

} catch (err) {
// if we explode looking up locale just noop
// we'll keep using the default language 'en'.
Expand Down