Skip to content

Commit

Permalink
Merge pull request #546 from digitalpatrol/fix/count-unique-and-plura…
Browse files Browse the repository at this point in the history
…l-keys-ns

fix: count keys for each namespace instead of global
  • Loading branch information
karellm committed Apr 3, 2022
2 parents 9c062ec + ee7be6b commit ae988a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/transform.js
Expand Up @@ -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, {
Expand All @@ -164,29 +164,35 @@ 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)
} else if (conflict === 'value') {
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
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
29 changes: 29 additions & 0 deletions test/parser.test.js
Expand Up @@ -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({
Expand Down

0 comments on commit ae988a5

Please sign in to comment.