Skip to content

Commit

Permalink
forward ready in hoc, renderprop
Browse files Browse the repository at this point in the history
  • Loading branch information
jamuhl committed Feb 14, 2019
1 parent dcb7ea4 commit a46263e
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 10.1.1

- forward ready state in withTranslation, Translation

### 10.1.0

- better naming for Wrappers in HOC for easier use of react debugger in console
Expand Down
5 changes: 3 additions & 2 deletions example/test-jest/__mocks__/react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ const renderNodes = reactNodes => {
if (hasChildren(child)) {
const inner = renderNodes(getChildren(child));
return React.cloneElement(child, { ...child.props, key: i }, inner);
} if (typeof child === 'object' && !isElement) {
}
if (typeof child === 'object' && !isElement) {
return Object.keys(child).reduce((str, childKey) => `${str}${child[childKey]}`, '');
}

return child;
});
};

const useMock = [k=>k, {}];
const useMock = [k => k, {}];
useMock.t = k => k;
useMock.i18n = {};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-i18next",
"version": "10.1.0",
"version": "10.1.1",
"description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
"main": "dist/commonjs/index.js",
"types": "src/index.d.ts",
Expand Down
17 changes: 10 additions & 7 deletions react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@

if (!i18n) {
warnOnce('You will need pass in an i18next instance by using i18nextReactModule');
const retNotReady = [k => k, {}];
const retNotReady = [k => k, {}, true];

retNotReady.t = k => k;

Expand Down Expand Up @@ -729,13 +729,15 @@
return function Extend(WrappedComponent) {
function I18nextWithTranslation(props) {
const _useTranslation = useTranslation(ns, props),
_useTranslation2 = _slicedToArray(_useTranslation, 2),
_useTranslation2 = _slicedToArray(_useTranslation, 3),
t = _useTranslation2[0],
i18n = _useTranslation2[1];
i18n = _useTranslation2[1],
ready = _useTranslation2[2];

return React__default.createElement(WrappedComponent, _objectSpread({}, props, {
t,
i18n
i18n,
tReady: ready
}));
}

Expand All @@ -749,14 +751,15 @@
options = _objectWithoutProperties(props, ["ns", "children"]);

const _useTranslation = useTranslation(ns, options),
_useTranslation2 = _slicedToArray(_useTranslation, 2),
_useTranslation2 = _slicedToArray(_useTranslation, 3),
t = _useTranslation2[0],
i18n = _useTranslation2[1];
i18n = _useTranslation2[1],
ready = _useTranslation2[2];

return children(t, {
i18n,
lng: i18n.language
});
}, ready);
}

function I18nextProvider(_ref) {
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions src/Translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { useTranslation } from './useTranslation';

export function Translation(props) {
const { ns, children, ...options } = props;
const [t, i18n] = useTranslation(ns, options);
const [t, i18n, ready] = useTranslation(ns, options);

return children(t, {
i18n,
lng: i18n.language,
});
return children(
t,
{
i18n,
lng: i18n.language,
},
ready,
);
}
2 changes: 1 addition & 1 deletion src/useTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 i18nextReactModule');
const retNotReady = [k => k, {}];
const retNotReady = [k => k, {}, true];
retNotReady.t = k => k;
retNotReady.i18n = {};
retNotReady.ready = true;
Expand Down
3 changes: 2 additions & 1 deletion src/withTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { useTranslation } from './useTranslation';
export function withTranslation(ns) {
return function Extend(WrappedComponent) {
function I18nextWithTranslation(props) {
const [t, i18n] = useTranslation(ns, props);
const [t, i18n, ready] = useTranslation(ns, props);

return React.createElement(WrappedComponent, {
...props,
t,
i18n,
tReady: ready,
});
}

Expand Down
4 changes: 1 addition & 3 deletions test/typescript/examples.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function MyComponent() {

// Component using the Trans component without children (Self-closing)
function MyComponentWithoutChildren() {
return (
<Trans i18nKey="description.part1" />
);
return <Trans i18nKey="description.part1" />;
}

// page uses the hook
Expand Down

0 comments on commit a46263e

Please sign in to comment.