Repository: i18next/i18next-parser
Pull request #546 (2022-04-02 18:52:33)

fix: count keys for each namespace instead of global
### Why am I submitting this PR

When using the verbose option, keys are counted globally while presented as if it is per file. This merge request make sure that statistics are given for each namespace

### Does it fix an existing ticket?

Yes,
As a side effect of the incorrect count for the number of unique/plural keys the fail-on-update bug as described in #489 is not working correctly. This should now be fixed too.

### Checklist

- [x ] only relevant code is changed (make a diff before you submit the PR)
- [ x] tests are included and pass: `yarn test` (see [details here](https://github.com/i18next/i18next-parser/blob/master/docs/development.md#tests))
- [ x] documentation is changed or added



Patch:
diff --git a/src/transform.js b/src/transform.js
index 8b2090d..cb49e2b 100644
--- a/src/transform.js
+++ b/src/transform.js
@@ -148,8 +148,8 @@ export default class i18nTransform extends Transform {
       const catalog = {}
       const resetAndFlag = this.options.resetDefaultValueLocale === locale
 
-      let countWithPlurals = 0
-      let uniqueCount = this.entries.length
+      let countWithPlurals = {}
+      let uniqueCount = {}
 
       const transformEntry = (entry, suffix) => {
         const { duplicate, conflict } = dotPathToHash(entry, catalog, {
@@ -164,7 +164,6 @@ export default class i18nTransform extends Transform {
         })
 
         if (duplicate) {
-          uniqueCount -= 1
           if (conflict === 'key') {
             const warning = `Found translation key already mapped to a map or parent of new key already mapped to a string: ${entry.key}`
             this.warn(warning)
@@ -172,21 +171,28 @@ export default class i18nTransform extends Transform {
             const warning = `Found same keys with different values: ${entry.key}`
             this.warn(warning)
           }
-        } else {
-          countWithPlurals += 1
         }
       }
 
       // generates plurals according to i18next rules: key_zero, key_one, key_two, key_few, key_many and key_other
       for (const entry of this.entries) {
+        if (uniqueCount[entry.namespace] === undefined) {
+          uniqueCount[entry.namespace] = 0
+        }
+        if (countWithPlurals[entry.namespace] === undefined) {
+          countWithPlurals[entry.namespace] = 0
+        }
+        uniqueCount[entry.namespace] += 1
         if (entry.count !== undefined) {
           this.i18next.services.pluralResolver
             .getSuffixes(locale, { ordinal: entry.ordinal })
             .forEach((suffix) => {
+              countWithPlurals[entry.namespace] += 1
               transformEntry(entry, suffix)
             })
         } else {
           transformEntry(entry)
+          countWithPlurals[entry.namespace] += 1
         }
       }
 
@@ -244,9 +250,9 @@ export default class i18nTransform extends Transform {
         if (this.options.verbose) {
           console.log(`[${locale}] ${namespace}\n`)
           console.log(
-            `Unique keys: ${uniqueCount} (${countWithPlurals} with plurals)`
+            `Unique keys: ${uniqueCount[namespace]} (${countWithPlurals[namespace]} with plurals)`
           )
-          const addCount = countWithPlurals - mergeCount
+          const addCount = countWithPlurals[namespace] - mergeCount
           console.log(`Added keys: ${addCount}`)
           console.log(`Restored keys: ${restoreCount}`)
           if (this.options.keepRemoved) {
@@ -261,7 +267,7 @@ export default class i18nTransform extends Transform {
         }
 
         if (this.options.failOnUpdate) {
-          const addCount = countWithPlurals - mergeCount
+          const addCount = countWithPlurals[namespace] - mergeCount
           if (addCount + restoreCount + oldCount !== 0) {
             this.parserHadUpdate = true
             continue


Test patch:
diff --git a/test/parser.test.js b/test/parser.test.js
index e30c0f0..56d9df7 100644
--- a/test/parser.test.js
+++ b/test/parser.test.js
@@ -755,6 +755,35 @@ describe('parser', () => {
         console.log.restore()
       })
 
+      it('logs number of unique keys', (done) => {
+        const i18nextParser = new i18nTransform({
+          verbose: true,
+          output: 'test/locales/$LOCALE/$NAMESPACE.json',
+          locales: ['en'],
+        })
+        const fakeFile = new Vinyl({
+          contents: Buffer.from(
+            `t('key', {
+              ns: 'namespace1',
+            })
+            t('key', {
+              ns: 'namespace2',
+            })
+            `
+          ),
+          path: 'file.js',
+        })
+
+        i18nextParser.on('data', () => {})
+
+        i18nextParser.once('end', () => {
+          assert(console.log.calledWith('Unique keys: 1 (1 with plurals)'))
+          done()
+        })
+
+        i18nextParser.end(fakeFile)
+      })
+
       describe('with defaultResetLocale', () => {
         it('logs the number of values reset', (done) => {
           const i18nextParser = new i18nTransform({