Repository: i18next/i18next-parser
Pull request #196 (2020-01-13 10:15:28)

Use Trans component ns (namespace) prop
Fix for #192 


Patch:
diff --git a/dist/lexers/jsx-lexer.js b/dist/lexers/jsx-lexer.js
index bae6e86..4658e3c 100644
--- a/dist/lexers/jsx-lexer.js
+++ b/dist/lexers/jsx-lexer.js
@@ -43,11 +43,13 @@ JsxLexer = function (_JavascriptLexer) {_inherits(JsxLexer, _JavascriptLexer);
     node, sourceText) {var _this3 = this;
       var tagNode = node.openingElement || node;
 
-      var getKey = function getKey(node) {
-        var attribute = node.attributes.properties.find(function (attr) {return attr.name.text === _this3.attr;});
+      var getPropValue = function getPropValue(node, tagName) {
+        var attribute = node.attributes.properties.find(function (attr) {return attr.name.text === tagName;});
         return attribute && attribute.initializer.text;
       };
 
+      var getKey = function getKey(node) {return getPropValue(node, _this3.attr);};
+
       if (tagNode.tagName.text === "Trans") {
         var entry = {};
         entry.key = getKey(tagNode);
@@ -62,6 +64,11 @@ JsxLexer = function (_JavascriptLexer) {_inherits(JsxLexer, _JavascriptLexer);
           }
         }
 
+        var namespace = getPropValue(tagNode, 'ns');
+        if (namespace) {
+          entry.namespace = namespace;
+        }
+
         return entry.key ? entry : null;
       } else
       if (tagNode.tagName.text === "Interpolate") {
diff --git a/src/lexers/jsx-lexer.js b/src/lexers/jsx-lexer.js
index 0ea0658..66671ed 100644
--- a/src/lexers/jsx-lexer.js
+++ b/src/lexers/jsx-lexer.js
@@ -43,11 +43,13 @@ export default class JsxLexer extends JavascriptLexer {
   jsxExtractor(node, sourceText) {
     const tagNode = node.openingElement || node
 
-    const getKey = (node) => {
-      const attribute = node.attributes.properties.find(attr => attr.name.text === this.attr)
+    const getPropValue = (node, tagName) => {
+      const attribute = node.attributes.properties.find(attr => attr.name.text === tagName)
       return attribute && attribute.initializer.text
     }
 
+    const getKey = (node) => getPropValue(node, this.attr)
+
     if (tagNode.tagName.text === "Trans") {
       const entry = {}
       entry.key = getKey(tagNode)
@@ -62,6 +64,11 @@ export default class JsxLexer extends JavascriptLexer {
         }
       }
 
+      const namespace = getPropValue(tagNode, 'ns')
+      if (namespace) {
+        entry.namespace = namespace
+      }
+
       return entry.key ? entry : null
     }
     else if (tagNode.tagName.text === "Interpolate") {


Test patch:
diff --git a/test/lexers/jsx-lexer.test.js b/test/lexers/jsx-lexer.test.js
index 4e1dc6b..dd4dea7 100644
--- a/test/lexers/jsx-lexer.test.js
+++ b/test/lexers/jsx-lexer.test.js
@@ -119,6 +119,14 @@ describe('JsxLexer', () => {
       assert.equal(Lexer.extract(content)[0].defaultValue, 'Some Interpolated Content')
       done()
     })
+
+    it('uses the ns (namespace) prop', (done) => {
+      const Lexer = new JsxLexer()
+      const content = `<Trans ns="foo">bar</Trans>`
+      assert.deepEqual(Lexer.extract(content), [{ key: 'bar', defaultValue: 'bar', namespace: 'foo' }])
+      done()
+    })
+
   })
 
   describe('supports TypeScript', () => {