Skip to content

Commit

Permalink
don't use defaults prop as default key (#1664)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicegamer7 authored Aug 22, 2023
1 parent 52d514c commit 079938c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,11 @@ export function Trans({
let namespaces = ns || t.ns || (i18n.options && i18n.options.defaultNS);
namespaces = typeof namespaces === 'string' ? [namespaces] : namespaces || ['translation'];

const nodeAsString = nodesToString(children, reactI18nextOptions);
const defaultValue =
defaults ||
nodesToString(children, reactI18nextOptions) ||
reactI18nextOptions.transEmptyNodeValue ||
i18nKey;
defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
const { hashTransKey } = reactI18nextOptions;
const key = i18nKey || (hashTransKey ? hashTransKey(defaultValue) : defaultValue);
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString) : nodeAsString);
const interpolationOverride = values
? tOptions.interpolation
: { interpolation: { ...tOptions.interpolation, prefix: '#$?', suffix: '?$#' } };
Expand Down
2 changes: 2 additions & 0 deletions test/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ i18n.init({
transTestCustomUnescapeSecond: 'Vertrauens­kennwert',
testTransWithCtx: 'Go <1>there</1>.',
testTransWithCtx_home: 'Go <1>home</1>.',
'You have {{count}} message_one': 'You have {{count}} message',
'You have {{count}} message_other': 'You have {{count}} messages',
deepPath: {
deepKey1: 'value1',
},
Expand Down
19 changes: 19 additions & 0 deletions test/trans.render.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,22 @@ describe('trans with context property', () => {
`);
});
});

describe('trans with defaults property and children', () => {
function TestComponent() {
return (
<Trans count={3} defaults="This property should not get used">
You have {{ count: 3 }} message
</Trans>
);
}

it('should render correct content', () => {
const { container } = render(<TestComponent />);
expect(container.firstChild).toMatchInlineSnapshot(`
<div>
You have 3 messages
</div>
`);
});
});

0 comments on commit 079938c

Please sign in to comment.