From d9964bdc85717805aea374b82de9b0edb37a0bde Mon Sep 17 00:00:00 2001 From: Aral Roca Gomez Date: Wed, 11 Dec 2019 15:36:38 +0100 Subject: [PATCH] Fix fallback when the i18n entry is not found (#22) --- __tests__/useTranslation.test.js | 42 ++++++++++++++++++++++++++++++++ package.json | 2 +- src/I18nProvider.js | 2 +- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/__tests__/useTranslation.test.js b/__tests__/useTranslation.test.js index 796a8bf4..3d693d0f 100644 --- a/__tests__/useTranslation.test.js +++ b/__tests__/useTranslation.test.js @@ -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( + + + + ) + 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( + + + + ) + expect(container.textContent).toBe(expected) + }) }) }) diff --git a/package.json b/package.json index cad1428c..8a97d6b3 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/I18nProvider.js b/src/I18nProvider.js index d1331941..349be218 100644 --- a/src/I18nProvider.js +++ b/src/I18nProvider.js @@ -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 (