Skip to content

Commit

Permalink
try to fix Trans handling with alwaysFormat set to true #1801
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Sep 14, 2024
1 parent 5b02140 commit f490ab4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 15.0.2

- try to fix Trans handling with alwaysFormat set to true [1801](https://github.com/i18next/react-i18next/issues/1801)

### 15.0.1

- revert arrow function in class property to address [this](https://github.com/i18next/react-i18next/commit/46e8ea5ff69325b73087811a2ce6a2b1faffa971#r145061161)
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@
...i18n.options.interpolation.defaultVariables
};
}
const interpolationOverride = values || count !== undefined || !children ? tOptions.interpolation : {
const interpolationOverride = values || count !== undefined && !i18n.options?.interpolation?.alwaysFormat || !children ? tOptions.interpolation : {
interpolation: {
...tOptions.interpolation,
prefix: '#$?',
Expand Down
2 changes: 1 addition & 1 deletion react-i18next.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/TransWithoutContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ export function Trans({
: { ...i18n.options.interpolation.defaultVariables };
}
const interpolationOverride =
values || count !== undefined || !children // if !children gets problems in future, undo that fix: https://github.com/i18next/react-i18next/issues/1729 by removing !children from this condition
values ||
(count !== undefined && !i18n.options?.interpolation?.alwaysFormat) || // https://github.com/i18next/react-i18next/issues/1719 + https://github.com/i18next/react-i18next/issues/1801
!children // if !children gets problems in future, undo that fix: https://github.com/i18next/react-i18next/issues/1729 by removing !children from this condition
? tOptions.interpolation
: { interpolation: { ...tOptions.interpolation, prefix: '#$?', suffix: '?$#' } };
const combinedTOpts = {
Expand Down
39 changes: 38 additions & 1 deletion test/trans.render.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest';
import { describe, it, expect, beforeAll, afterAll, beforeEach, afterEach } from 'vitest';
import React from 'react';
import { render, cleanup } from '@testing-library/react';
import i18n from './i18n';
Expand Down Expand Up @@ -891,6 +891,43 @@ describe('trans with formatting', () => {
});
});

describe('trans with formatting with alwaysFormat', () => {
let newI18n;

beforeEach(() => {
newI18n = i18n.createInstance();
newI18n.init({
interpolation: {
alwaysFormat: true,
escapeValue: false,
format: (value) => `(formatted ${value})`,
},
});
});

function TestComponent({ parent }) {
const name = 'Fritz';
return (
<Trans parent={parent} i18n={newI18n} count={3}>
number: {'{{count, INTEGER}}'}
<br />
name: {{ name }}
</Trans>
);
}

it('should render correct content', () => {
const { container } = render(<TestComponent />);
expect(container).toMatchInlineSnapshot(`
<div>
number: (formatted 3)
<br />
name: (formatted Fritz)
</div>
`);
});
});

describe('trans with undefined context property', () => {
function TestComponent({ parent }) {
return (
Expand Down

0 comments on commit f490ab4

Please sign in to comment.