Skip to content

Commit

Permalink
Fix Trans component when index is incorrect (#24)
Browse files Browse the repository at this point in the history
* Fix Trans component  when index is incorrect

* Fix test text
  • Loading branch information
aralroca authored Dec 12, 2019
1 parent 2c35821 commit cd69557
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
17 changes: 17 additions & 0 deletions __tests__/Trans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,22 @@ describe('Trans', () => {
expect(container.textContent).toContain(expectedText)
expect(container.innerHTML).toContain(expectedHTML)
})

test('should work without replacing the HTMLElement if the index is incorrectly', () => {
const i18nKey = 'common:test-html'
const expectedHTML = "test with bad index."
const common = {
"test-html": "test <10>with bad index</10>.",
}

const { container } = render(
<TestEnglish
namespaces={{ common }}
i18nKey={i18nKey}
components={[<b />]}
/>
)
expect(container.innerHTML).toContain(expectedHTML)
})
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.1.6",
"version": "0.1.7",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down
5 changes: 3 additions & 2 deletions src/Trans.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cloneElement, useMemo } from 'react'
import { cloneElement, useMemo, Fragment } from 'react'
import useTranslation from './useTranslation'

const tagRe = /<(\d+)>(.*?)<\/\1>|<(\d+)\/>/
Expand Down Expand Up @@ -28,7 +28,8 @@ function formatElements(
if (before) tree.push(before)

for (const [index, children, after] of getElements(parts)) {
const element = elements[index]
const element = elements[index] || <Fragment />

tree.push(
cloneElement(
element,
Expand Down
2 changes: 1 addition & 1 deletion src/appWithI18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import I18nProvider from './I18nProvider'

function getLang(ctx, config) {
const { req, asPath } = ctx
const { req, asPath = '' } = ctx

if (req) return req.query.lang || config.defaultLanguage

Expand Down

0 comments on commit cd69557

Please sign in to comment.