Skip to content

Commit

Permalink
Merge pull request #18 from transifex/react_T_params
Browse files Browse the repository at this point in the history
Reintroduce support for ICU parameters
  • Loading branch information
kbairak authored Aug 27, 2020
2 parents a149705 + a676ab5 commit 06deac8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/react/src/components/T.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import {
t, onEvent, offEvent, LOCALE_CHANGED,
} from '@transifex/native';

function T({ _str, _html, _inline }) {
function T({
_str, _html, _inline, ...props
}) {
const [translation, setTranslation] = useState('');

useEffect(() => {
function render() {
if (!_html) {
setTranslation(t(_str, { _html, _inline }));
setTranslation(t(_str, { _html, _inline, ...props }));
} else {
const result = t(_str, {
_html,
_inline,
_escapeVars: true,
...props,
});
const parentProps = { dangerouslySetInnerHTML: { __html: result } };
const parent = _inline ? 'span' : 'div';
Expand Down
6 changes: 6 additions & 0 deletions packages/react/tests/T.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe('T', () => {
expect(screen.queryByText(message)).toBeInTheDocument();
});

it('renders text with param', () => {
const message = 'Hello <b>{username}</b>';
render(<T _str={message} username="JohnDoe" />);
expect(screen.queryByText('Hello <b>JohnDoe</b>')).toBeInTheDocument();
});

it('renders HTML', () => {
const message = 'Hello <b>Unsafe HTML</b>';
render(<T _str={message} _html />);
Expand Down

0 comments on commit 06deac8

Please sign in to comment.