Skip to content

Commit

Permalink
Keep existing namespace when key is empty Fix #502
Browse files Browse the repository at this point in the history
  • Loading branch information
karellm committed Apr 29, 2022
1 parent e875d60 commit 299023e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/helpers.js
Expand Up @@ -28,7 +28,9 @@ function dotPathToHash(entry, target = {}, options = {}) {

// There is no key to process so we return an empty object
if (!key) {
target[entry.namespace] = {}
if (!target[entry.namespace]) {
target[entry.namespace] = {}
}
return { target, duplicate, conflict }
}

Expand Down
3 changes: 3 additions & 0 deletions test/locales/en/test_empty.json
@@ -0,0 +1,3 @@
{
"key": ""
}
24 changes: 24 additions & 0 deletions test/parser.test.js
Expand Up @@ -328,6 +328,30 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it.only('does not overwrite the namespace file if it already exists', (done) => {
let resultContent
const i18nextParser = new i18nTransform({
locales: ['en'],
defaultNamespace: 'test_empty',
})
const fakeFile = new Vinyl({
contents: Buffer.from("t('key'); t('');"),
path: 'file.js',
})

i18nextParser.on('data', (file) => {
if (file.relative.endsWith(path.normalize('en/test_empty.json'))) {
resultContent = JSON.parse(file.contents)
}
})
i18nextParser.on('end', () => {
assert.deepEqual(resultContent, { key: '' })
done()
})

i18nextParser.end(fakeFile)
})

it('applies withTranslation namespace globally', (done) => {
let result
const i18nextParser = new i18nTransform()
Expand Down

0 comments on commit 299023e

Please sign in to comment.