Skip to content

Commit

Permalink
Fix fallback when the i18n entry is not found (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Dec 11, 2019
1 parent 9b1fd63 commit d9964bd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
42 changes: 42 additions & 0 deletions __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,47 @@ describe('useTranslation', () => {
)
expect(container.textContent).toContain(expected)
})

test('should return the key as fallback', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t('ns:template-string')
return (
<>
{test} | {typeof test}
</>
)
}

const expected = 'ns:template-string | string'

const { container } = render(
<I18nProvider lang="en" namespaces={{}} >
<Inner />
</I18nProvider>
)
expect(container.textContent).toBe(expected)
})

test('should return the key as fallback using a template string', () => {
const Inner = () => {
const { t } = useTranslation()
const test = t`ns:template-string`
return (
<>
{test} | {typeof test}
</>
)
}

const expected = 'ns:template-string | string'

const { container } = render(
<I18nProvider lang="en" namespaces={{}} >
<Inner />
</I18nProvider>
)
expect(container.textContent).toBe(expected)
})
})
})
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.4",
"version": "0.1.5",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion src/I18nProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function I18nProvider({ lang, namespaces = {}, children }) {
const dic = allNamespaces[namespace] || {}
const keyWithPlural = plural(dic, i18nKey, query)

return interpolation(dic[keyWithPlural], query) || key
return interpolation(dic[keyWithPlural], query) || k
}

return (
Expand Down

0 comments on commit d9964bd

Please sign in to comment.