Skip to content

Commit

Permalink
fixed graceful fallback for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ywongau committed Nov 29, 2019
1 parent b2c0421 commit 85c60fd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/useTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export function useTranslation(ns, props = {}) {
if (i18n && !i18n.reportNamespaces) i18n.reportNamespaces = new ReportNamespaces();
if (!i18n) {
warnOnce('You will need pass in an i18next instance by using initReactI18next');
const retNotReady = [k => k, {}, false];
retNotReady.t = k => k;
const notReadyT = k => (Array.isArray(k) ? k[k.length - 1] : k);
const retNotReady = [notReadyT, {}, false];
retNotReady.t = notReadyT;
retNotReady.i18n = {};
retNotReady.ready = false;
return retNotReady;
Expand Down
8 changes: 7 additions & 1 deletion test/useTranslation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ describe('useTranslation', () => {
expect(typeof t).toBe('function');
expect(i18n).toEqual({});

return <div>{t('key1')}</div>;
return (
<>
<div>{t('key1')}</div>
<div>{t(['doh', 'Human friendly fallback'])}</div>
</>
);
}

it('should render content fallback', () => {
console.warn = jest.fn();
const wrapper = mount(<TestComponent />, {});
// console.log(wrapper.debug());
expect(wrapper.contains(<div>key1</div>)).toBe(true);
expect(wrapper.contains(<div>Human friendly fallback</div>)).toBe(true);
expect(console.warn).toHaveBeenCalled();
});
});
Expand Down

0 comments on commit 85c60fd

Please sign in to comment.