Skip to content

Commit

Permalink
Make sure empty namespace files are an empty object (not an empty str…
Browse files Browse the repository at this point in the history
…ing) Fix #273
  • Loading branch information
karellm committed May 17, 2021
1 parent 8540711 commit 16e13fd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* different value, or `false`.
*/
function dotPathToHash(entry, target = {}, options = {}) {
let conflict = false
let duplicate = false
let path = entry.keyWithNamespace
if (options.suffix || options.suffix === 0) {
path += `${options.pluralSeparator}${options.suffix}`
Expand All @@ -24,6 +26,12 @@ function dotPathToHash(entry, target = {}, options = {}) {
entry.keyWithNamespace.length
)

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

const defaultValue =
typeof options.value === 'function'
? options.value(options.locale, entry.namespace, key)
Expand Down Expand Up @@ -59,7 +67,6 @@ function dotPathToHash(entry, target = {}, options = {}) {

const segments = path.split(separator)
let inner = target
let conflict = false
for (let i = 0; i < segments.length - 1; i += 1) {
const segment = segments[i]
if (segment) {
Expand All @@ -78,7 +85,7 @@ function dotPathToHash(entry, target = {}, options = {}) {
if (oldValue !== undefined && oldValue !== newValue) {
conflict = typeof oldValue !== typeof newValue ? 'key' : 'value'
}
const duplicate = oldValue !== undefined || conflict !== false
duplicate = oldValue !== undefined || conflict !== false

if (options.customValueTemplate) {
inner[lastSegment] = {}
Expand Down
9 changes: 9 additions & 0 deletions test/helpers/dotPathToHash.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ describe('dotPathToHash helper function', () => {
done()
})

it('handles an empty namespace', (done) => {
const { target, duplicate } = dotPathToHash(
{ keyWithNamespace: 'ns.', namespace: 'ns' }
)
assert.deepEqual(target, { ns: {} })
assert.equal(duplicate, false)
done()
})

it('handles a target hash', (done) => {
const { target, duplicate } = dotPathToHash(
{ keyWithNamespace: 'one.two.three' },
Expand Down
1 change: 1 addition & 0 deletions test/manual/html.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
>asd</p>
<p data-i18n="seventh" data-i18n-options='{"defaultValue": "foo"}'>Seventh</p>
<p data-i18n="seventh" data-i18n-options='{"defaultValue": "bar"}'>Seventh</p>
<p data-i18n="empty:">Empty</p>
</div>
</body>
</html>
35 changes: 35 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,41 @@ describe('parser', () => {
i18nextParser.end(fakeFile)
})

it('creates an empty namespace file if no key is provided', (done) => {
let results = []
let resultContent
const i18nextParser = new i18nTransform({
locales: ['en'],
defaultNamespace: 'default',
})
const fakeFile = new Vinyl({
contents: Buffer.from("t('empty:');"),
path: 'file.js',
})

i18nextParser.on('data', (file) => {
if (file.relative.endsWith(path.normalize('en/empty.json'))) {
resultContent = JSON.parse(file.contents)
}
results.push(file.relative.replace(/locales[\//\\]/, ''))
})
i18nextParser.on('end', () => {
const expectedFiles = [
'en/empty.json'
]
let length = expectedFiles.length

assert.equal(results.length, 1)
assert.deepEqual(resultContent, {})
for (const filename of expectedFiles) {
assert.include(results, path.normalize(filename))
if (!--length) done()
}
})

i18nextParser.end(fakeFile)
})

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

0 comments on commit 16e13fd

Please sign in to comment.