Skip to content

Commit

Permalink
Fix fallback of very nested translations (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Apr 7, 2020
1 parent 7243a68 commit 22676b8
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 611 deletions.
16 changes: 16 additions & 0 deletions __tests__/useTranslation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ describe('useTranslation', () => {
expect(container.textContent).toContain(expected)
})

test('should return the key as fallback using wrong the very nested translations', () => {
const i18nKey = 'ns:grandfather.parent.this.is.very.nested.example'
const expected = 'ns:grandfather.parent.this.is.very.nested.example'
const nested = {
grandfather: {
parent: {
child: 'I am the child',
},
},
}
const { container } = render(
<TestEnglish namespaces={{ ns: nested }} i18nKey={i18nKey} />
)
expect(container.textContent).toContain(expected)
})

test('should return the key as fallback WITH PROVIDER', () => {
const Inner = () => {
const { t } = useTranslation()
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.14.2",
"version": "0.14.3",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -42,15 +42,15 @@
"@babel/core": "7.9.0",
"@babel/preset-env": "7.9.0",
"@testing-library/react": "10.0.2",
"babel-jest": "25.2.4",
"babel-jest": "25.2.6",
"babel-plugin-transform-es2015-modules-commonjs": "6.26.2",
"babel-preset-minify": "0.5.1",
"cross-env": "7.0.2",
"express": "4.17.1",
"husky": "4.2.3",
"jest": "25.2.4",
"jest": "25.2.7",
"next": "9.3.4",
"prettier": "2.0.2",
"prettier": "2.0.4",
"pretty-quick": "2.0.1",
"react": "16.13.1",
"react-dom": "16.13.1",
Expand Down
2 changes: 1 addition & 1 deletion src/I18nProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const NsContext = createContext({})
* Get value from key (allow nested keys as parent.children)
*/
function getDicValue(dic, key = '') {
const value = key.split('.').reduce((val, key) => val[key], dic)
const value = key.split('.').reduce((val, key) => val[key] || {}, dic)
return typeof value === 'string' ? value : undefined
}

Expand Down
Loading

0 comments on commit 22676b8

Please sign in to comment.