Repository: yargs/yargs
Pull request #1183 (2018-07-17 06:26:48)

fix: translation not working when using __ with a single parameter
Closes yargs/yargs#1101

nexdrew: @jjoselv Good find! I verified locally that this one i18n message was not being properly translated and that your change fixes it. I then wrote a test for it and pushed it to your fork, which added it to this PR.

On top of this, would you be able to provide any proper translations of the i18n string in other languages? If so, you could add those translations to this PR, if you want. Otherwise, we can worry about that later.

Thanks again!
jjoselv: If all is well, can this PR be merged by someone please?

Patch:
diff --git a/.gitignore b/.gitignore
index 3e4ea0e..f8a81be 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,4 @@ node_modules/
 test.js
 coverage
 package-lock.json
+.npmrc
diff --git a/lib/command.js b/lib/command.js
index bbd986e..ea7aa3d 100644
--- a/lib/command.js
+++ b/lib/command.js
@@ -19,7 +19,8 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
     let aliases = []
     handler = handler || (() => {})
     middlewares = middlewares || []
-    middlewares = globalMiddleware.concat(middlewares)
+    globalMiddleware.push(...middlewares)
+    middlewares = globalMiddleware
     if (Array.isArray(cmd)) {
       aliases = cmd.slice(1)
       cmd = cmd[0]
diff --git a/lib/validation.js b/lib/validation.js
index d72ad52..e9bbb12 100644
--- a/lib/validation.js
+++ b/lib/validation.js
@@ -292,7 +292,7 @@ module.exports = function validation (yargs, usage, y18n) {
           // we default keys to 'undefined' that have been configured, we should not
           // apply conflicting check unless they are a value other than 'undefined'.
           if (value && argv[key] !== undefined && argv[value] !== undefined) {
-            usage.fail(__(`Arguments ${key} and ${value} are mutually exclusive`))
+            usage.fail(__('Arguments %s and %s are mutually exclusive', key, value))
           }
         })
       }
diff --git a/locales/pirate.json b/locales/pirate.json
index 1f4e19e..dcb5cb7 100644
--- a/locales/pirate.json
+++ b/locales/pirate.json
@@ -8,5 +8,6 @@
     "other": "Ye be havin' to set the followin' arguments land lubber: %s"
   },
   "Show help": "Parlay this here code of conduct",
-  "Show version number": "'Tis the version ye be askin' fer"
+  "Show version number": "'Tis the version ye be askin' fer",
+  "Arguments %s and %s are mutually exclusive" : "Yon scurvy dogs %s and %s be as bad as rum and a prudish wench"
 }


Test patch:
diff --git a/test/middleware.js b/test/middleware.js
index 764b1b0..0423768 100644
--- a/test/middleware.js
+++ b/test/middleware.js
@@ -53,4 +53,31 @@ describe('middleware', () => {
       .exitProcess(false) // defaults to true.
       .parse()
   })
+
+  it('should be able to register middleware regardless of when middleware is called', function (done) {
+    yargs(['mw'])
+      .middleware([
+        function (argv) {
+          argv.mw1 = 'mw1'
+        }
+      ])
+      .command(
+        'mw',
+        'adds func list to middleware',
+        function () {},
+        function (argv) {
+          // we should get the argv filled with data from the middleware
+          argv.mw1.should.equal('mw1')
+          argv.mw2.should.equal('mw2')
+          return done()
+        }
+      )
+      .middleware([
+        function (argv) {
+          argv.mw2 = 'mw2'
+        }
+      ])
+      .exitProcess(false) // defaults to true.
+      .parse()
+  })
 })
diff --git a/test/validation.js b/test/validation.js
index c9d0593..788ca70 100644
--- a/test/validation.js
+++ b/test/validation.js
@@ -3,7 +3,7 @@
 
 const checkUsage = require('./helpers/utils').checkOutput
 const expect = require('chai').expect
-const yargs = require('../')
+let yargs = require('../')
 
 require('chai').should()
 
@@ -285,6 +285,28 @@ describe('validation tests', () => {
         })
         .parse()
     })
+
+    function loadLocale (locale) {
+      delete require.cache[require.resolve('../')]
+      delete require.cache[require.resolve('os-locale')]
+      yargs = require('../')
+      process.env.LC_ALL = locale
+    }
+
+    it('should use appropriate translation', done => {
+      loadLocale('pirate')
+      try {
+        yargs(['-f', '-b'])
+          .conflicts('f', 'b')
+          .fail(msg => {
+            msg.should.equal('Yon scurvy dogs f and b be as bad as rum and a prudish wench')
+            return done()
+          })
+          .parse()
+      } finally {
+        loadLocale('en_US.UTF-8')
+      }
+    })
   })
 
   describe('demand', () => {