Skip to content

Commit

Permalink
Fix stats with plural forms
Browse files Browse the repository at this point in the history
  • Loading branch information
karellm committed Apr 3, 2022
1 parent ae988a5 commit 451205a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 21 deletions.
42 changes: 23 additions & 19 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,17 @@ export default class i18nTransform extends Transform {
const catalog = {}
const resetAndFlag = this.options.resetDefaultValueLocale === locale

let countWithPlurals = {}
let uniqueCount = {}
let uniquePluralsCount = {}

const transformEntry = (entry, suffix) => {
if (uniqueCount[entry.namespace] === undefined) {
uniqueCount[entry.namespace] = 0
}
if (uniquePluralsCount[entry.namespace] === undefined) {
uniquePluralsCount[entry.namespace] = 0
}

const { duplicate, conflict } = dotPathToHash(entry, catalog, {
suffix,
locale,
Expand All @@ -165,34 +172,31 @@ export default class i18nTransform extends Transform {

if (duplicate) {
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)
this.warn(
`Found translation key already mapped to a map or parent of ` +
`new key already mapped to a string: ${entry.key}`
)
} else if (conflict === 'value') {
const warning = `Found same keys with different values: ${entry.key}`
this.warn(warning)
this.warn(`Found same keys with different values: ${entry.key}`)
}
} else {
uniqueCount[entry.namespace] += 1
if (suffix) {
uniquePluralsCount[entry.namespace] += 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 @@ -248,11 +252,11 @@ export default class i18nTransform extends Transform {
transferValues(oldKeys, oldCatalog)

if (this.options.verbose) {
console.log(`[${locale}] ${namespace}\n`)
console.log(`[${locale}] ${namespace}`)
console.log(
`Unique keys: ${uniqueCount[namespace]} (${countWithPlurals[namespace]} with plurals)`
`Unique keys: ${uniqueCount[namespace]} (${uniquePluralsCount[namespace]} are plurals)`
)
const addCount = countWithPlurals[namespace] - mergeCount
const addCount = uniqueCount[namespace] - mergeCount
console.log(`Added keys: ${addCount}`)
console.log(`Restored keys: ${restoreCount}`)
if (this.options.keepRemoved) {
Expand All @@ -263,11 +267,11 @@ export default class i18nTransform extends Transform {
if (this.options.resetDefaultValueLocale) {
console.log(`Reset keys: ${resetCount}`)
}
console.log()
console.log('')
}

if (this.options.failOnUpdate) {
const addCount = countWithPlurals[namespace] - mergeCount
const addCount = uniqueCount[namespace] - mergeCount
if (addCount + restoreCount + oldCount !== 0) {
this.parserHadUpdate = true
continue
Expand Down
6 changes: 6 additions & 0 deletions test/locales/en/test_log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"was_present": "first",
"was_present_but_not_plural": "bla",
"was_present_and_plural_but_no_more_one": "bla",
"was_present_and_plural_but_no_more_other": "bla"
}
50 changes: 48 additions & 2 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ describe('parser', () => {
console.log.restore()
})

it('logs number of unique keys', (done) => {
it('logs stats unique keys per namespace', (done) => {
const i18nextParser = new i18nTransform({
verbose: true,
output: 'test/locales/$LOCALE/$NAMESPACE.json',
Expand All @@ -766,6 +766,17 @@ describe('parser', () => {
`t('key', {
ns: 'namespace1',
})
t('key', {
ns: 'namespace1'
})
t('key2', {
ns: 'namespace1',
count: 1
})
t('key2', {
ns: 'namespace1',
count: 2
})
t('key', {
ns: 'namespace2',
})
Expand All @@ -777,7 +788,42 @@ describe('parser', () => {
i18nextParser.on('data', () => {})

i18nextParser.once('end', () => {
assert(console.log.calledWith('Unique keys: 1 (1 with plurals)'))
assert(console.log.calledWith('Unique keys: 3 (2 are plurals)')) // namespace1
assert(console.log.calledWith('Unique keys: 1 (0 are plurals)')) // namespace2
done()
})

i18nextParser.end(fakeFile)
})

it('logs added and removed keys', (done) => {
const i18nextParser = new i18nTransform({
verbose: true,
output: 'test/locales/$LOCALE/$NAMESPACE.json',
locales: ['en'],
})
const fakeFile = new Vinyl({
contents: Buffer.from(
`
t('test_log:was_present')
t('test_log:was_not_present')
t('test_log:was_present_but_not_plural', {
count: 1
})
t('test_log:was_not_present_and_plural', {
count: 1
})
t('test_log:was_present_and_plural_but_no_more')
`
),
path: 'file.js',
})

i18nextParser.on('data', () => {})

i18nextParser.once('end', () => {
assert(console.log.calledWith('Added keys: 6'))
assert(console.log.calledWith('Removed keys: 3'))
done()
})

Expand Down

0 comments on commit 451205a

Please sign in to comment.