Skip to content

Commit 79fa4d2

Browse files
author
Nikos Vasileiou
authored
Merge pull request #205 from transifex/react-hooks-tx-instance
Update React hooks to pass custom tx instance
2 parents e7c0547 + efe4322 commit 79fa4d2

7 files changed

Lines changed: 127 additions & 16 deletions

File tree

packages/react/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,23 @@ function Capitalized() {
172172
}
173173
```
174174

175+
Optionally `useT` can take as param a custom Native Instance:
176+
177+
```javascript
178+
import { useT } from '@transifex/react';
179+
import { createNativeInstance } from '@transifex/native';
180+
181+
const customTX = createNativeInstance({
182+
token: 'token',
183+
secret: 'secret',
184+
});
185+
186+
function Component() {
187+
const t = useT(customTX);
188+
// ...
189+
}
190+
```
191+
175192
## `useLanguages` hook
176193

177194
Returns a state variable that will eventually hold the supported languages of
@@ -195,6 +212,23 @@ function LanguageList () {
195212
}
196213
```
197214

215+
Optionally `useLanguages` can take as param a custom Native Instance:
216+
217+
```javascript
218+
import { useT } from '@transifex/react';
219+
import { createNativeInstance } from '@transifex/native';
220+
221+
const customTX = createNativeInstance({
222+
token: 'token',
223+
secret: 'secret',
224+
});
225+
226+
function Component() {
227+
const languages = useLanguages(customTX);
228+
// ...
229+
}
230+
```
231+
198232
## `useLocale` hook
199233

200234
Returns a state variable with the currently selected locale.
@@ -211,6 +245,23 @@ function DisplayLocale () {
211245
}
212246
```
213247

248+
Optionally `useLocale` can take as param a custom Native Instance:
249+
250+
```javascript
251+
import { useT } from '@transifex/react';
252+
import { createNativeInstance } from '@transifex/native';
253+
254+
const customTX = createNativeInstance({
255+
token: 'token',
256+
secret: 'secret',
257+
});
258+
259+
function Component() {
260+
const locale = useLocale(customTX);
261+
// ...
262+
}
263+
```
264+
214265
## `useTX` hook
215266

216267
Returns a state variable with the Native instance.
@@ -344,6 +395,23 @@ function Inner({ ready }) {
344395
}
345396
```
346397

398+
Optionally `useTranslations` can take as a second param a custom Native Instance:
399+
400+
```javascript
401+
import { useT } from '@transifex/react';
402+
import { createNativeInstance } from '@transifex/native';
403+
404+
const customTX = createNativeInstance({
405+
token: 'token',
406+
secret: 'secret',
407+
});
408+
409+
function Component() {
410+
const { ready } = useTranslations('inner', customTX);
411+
// ...
412+
}
413+
```
414+
347415
## `TXProvider` provider
348416
If you need to use more than one Transifex Native instances - like for example if you have a component library - you can use this provider to pass the desired instance to the children components.
349417

packages/react/src/hooks/useLanguages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import { TXNativeContext } from '../context/TXNativeContext';
1919
* );
2020
* } */
2121

22-
export default function useLanguages() {
22+
export default function useLanguages(txInstance) {
2323
// Check for a different tx initialization
2424
const context = useContext(TXNativeContext);
25-
const instance = context.instance || tx;
25+
const instance = txInstance || context.instance || tx;
2626

2727
const [languages, setLanguages] = useState([]);
2828
useEffect(() => {

packages/react/src/hooks/useLocale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import { TXNativeContext } from '../context/TXNativeContext';
1919
* );
2020
* }
2121
*/
22-
export default function useLocale() {
22+
export default function useLocale(txInstance) {
2323
// Check for a different tx initialization
2424
const context = useContext(TXNativeContext);
25-
const instance = context.instance || tx;
25+
const instance = txInstance || context.instance || tx;
2626

2727
const [locale, setLocale] = useState(instance.getCurrentLocale());
2828

packages/react/src/hooks/useT.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import { TXNativeContext } from '../context/TXNativeContext';
2626
* return <span>{translation.toUpperCase()}</span>;
2727
* } */
2828

29-
export default function useT() {
29+
export default function useT(txInstance) {
3030
// Check for a different tx initialization
3131
const context = useContext(TXNativeContext);
32-
const instance = context.instance || tx;
32+
const instance = txInstance || context.instance || tx;
3333

3434
const [counter, setCounter] = useState(0);
3535
useEffect(() => {
@@ -48,7 +48,7 @@ export default function useT() {
4848
}, [setCounter, instance]);
4949

5050
return useCallback(
51-
(_str, props) => translateWithElements(_str, props, context),
52-
[context, counter],
51+
(_str, props) => translateWithElements(_str, props, instance),
52+
[instance, counter],
5353
);
5454
}

packages/react/src/hooks/useTranslations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
} from '@transifex/native';
1010
import { TXNativeContext } from '../context/TXNativeContext';
1111

12-
export default function useTranslations(filterTags) {
12+
export default function useTranslations(filterTags, txInstance) {
1313
// Check for a different tx initialization
1414
const context = useContext(TXNativeContext);
15-
const instance = context.instance || tx;
15+
const instance = txInstance || context.instance || tx;
1616

1717
const [ready, setReady] = useState(
1818
(instance.fetchedTags[instance.currentLocale] || []).indexOf(filterTags) !== -1,

packages/react/src/utils/translateWithElements.jsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ import { t } from '@transifex/native';
1414
* an array of React elements, each within its unique `key` property and, if
1515
* there are more than one, will be returned as `<>{result}</>`. */
1616

17-
function translateWithElements(_str, props, context) {
17+
function translateWithElements(_str, props, tx) {
1818
let _t = t;
19-
if (context && context.instance) {
20-
const _tx = context.instance;
21-
_t = _tx.t;
19+
20+
if (tx) {
21+
// backwards compatible check, in case tx is a provider context
22+
if (tx.instance && tx.instance.t) {
23+
_t = tx.instance.t;
24+
} else if (tx.t) {
25+
_t = tx.t;
26+
}
2227
}
2328

2429
const actualProps = {};

packages/react/tests/useT.test.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
render, screen, cleanup, fireEvent, act,
77
} from '@testing-library/react';
88
import '@testing-library/jest-dom';
9-
import { sendEvent, LOCALE_CHANGED } from '@transifex/native';
10-
import { useT } from '../src';
9+
import { sendEvent, LOCALE_CHANGED, createNativeInstance } from '@transifex/native';
10+
import { TXProvider, useT } from '../src';
1111

1212
describe('useT', () => {
1313
afterEach(() => {
@@ -76,4 +76,42 @@ describe('useT', () => {
7676
sendEvent(LOCALE_CHANGED);
7777
});
7878
});
79+
80+
it('renders with custom instance', () => {
81+
const instance = createNativeInstance();
82+
instance.translateLocale = () => 'hello from custom instance';
83+
84+
const MyComp = () => {
85+
const t = useT(instance);
86+
const message = t('hello');
87+
return (
88+
<>
89+
<p>{message}</p>
90+
</>
91+
);
92+
};
93+
render(<MyComp />);
94+
expect(screen.getByText('hello from custom instance')).toBeTruthy();
95+
});
96+
97+
it('renders with provider', () => {
98+
const instance = createNativeInstance();
99+
instance.translateLocale = () => 'hello from provider';
100+
101+
const MyComp = () => {
102+
const t = useT();
103+
const message = t('hello');
104+
return (
105+
<>
106+
<p>{message}</p>
107+
</>
108+
);
109+
};
110+
render(
111+
<TXProvider instance={instance}>
112+
<MyComp />
113+
</TXProvider>,
114+
);
115+
expect(screen.getByText('hello from provider')).toBeTruthy();
116+
});
79117
});

0 commit comments

Comments
 (0)