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

fix: count keys for each namespace instead of global #546

Merged
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
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