Skip to content

Commit

Permalink
feat: add context prop to Trans component #1464
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Mar 17, 2022
1 parent f8e197c commit cac1728
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 11.16.0

- fix: transSupportBasicHtmlNodes for keepArray check [1470](https://github.com/i18next/react-i18next/pull/1470)
- feat: add context prop to Trans component [1464](https://github.com/i18next/react-i18next/issues/1464)

### 11.15.7

- types: add nsSeparator to CustomTypeOptions [1471](https://github.com/i18next/react-i18next/pull/1471)
Expand Down
7 changes: 5 additions & 2 deletions react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
}

var _excluded = ["format"],
_excluded2 = ["children", "count", "parent", "i18nKey", "tOptions", "values", "defaults", "components", "ns", "i18n", "t", "shouldUnescape"];
_excluded2 = ["children", "count", "parent", "i18nKey", "context", "tOptions", "values", "defaults", "components", "ns", "i18n", "t", "shouldUnescape"];

function hasChildren(node, checkLength) {
if (!node) return false;
Expand Down Expand Up @@ -533,7 +533,7 @@
if (!children) return '';
var stringNode = '';
var childrenArray = getAsArray(children);
var keepArray = i18nOptions.transKeepBasicHtmlNodesFor || [];
var keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
childrenArray.forEach(function (child, childIndex) {
if (typeof child === 'string') {
stringNode += "".concat(child);
Expand Down Expand Up @@ -704,6 +704,7 @@
count = _ref.count,
parent = _ref.parent,
i18nKey = _ref.i18nKey,
context = _ref.context,
_ref$tOptions = _ref.tOptions,
tOptions = _ref$tOptions === void 0 ? {} : _ref$tOptions,
values = _ref.values,
Expand All @@ -730,6 +731,8 @@
return k;
};

if (context) tOptions.context = context;

var reactI18nextOptions = _objectSpread2(_objectSpread2({}, getDefaults()), i18n.options && i18n.options.react);

var namespaces = ns || t.ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Trans.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ export function Trans({
count,
parent,
i18nKey,
context,
tOptions = {},
values,
defaults,
Expand All @@ -297,6 +298,8 @@ export function Trans({

const t = tFromProps || i18n.t.bind(i18n) || ((k) => k);

if (context) tOptions.context = context;

const reactI18nextOptions = { ...getDefaults(), ...(i18n.options && i18n.options.react) };

// prepare having a namespace
Expand Down
2 changes: 2 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ i18n.init({
transTest3_overwrite:
'Result should be a clickable link <0 href="https://www.google.com">Google</0>',
transTestEscapedHtml: 'Escaped html should unescape correctly <0>&lt;&nbsp;&amp;&gt;</0>.',
testTransWithCtx: 'Go <1>there</1>.',
testTransWithCtx_home: 'Go <1>home</1>.',
},
other: {
transTest1: 'Another go <1>there</1>.',
Expand Down
23 changes: 23 additions & 0 deletions test/trans.render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,26 @@ it('transSupportBasicHtmlNodes: false should not keep the name of simple nodes',
</div>
`);
});

describe('trans with context property', () => {
const TestComponent = ({ parent }) => (
<Trans i18nKey="testTransWithCtx" context="home" parent={parent}>
Open <Link to="/msgs">here</Link>.
</Trans>
);

it('should render correct content', () => {
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
Go
<a
href="/msgs"
>
home
</a>
.
</div>
`);
});
});

0 comments on commit cac1728

Please sign in to comment.