Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed graceful fallback for keys #1010

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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