diff --git a/src/helpers.js b/src/helpers.js index ca2803dd..a81a67ca 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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 } } diff --git a/test/locales/en/test_empty.json b/test/locales/en/test_empty.json new file mode 100644 index 00000000..c991eb36 --- /dev/null +++ b/test/locales/en/test_empty.json @@ -0,0 +1,3 @@ +{ + "key": "" +} diff --git a/test/parser.test.js b/test/parser.test.js index 3b7b3877..228c0517 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -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()