Skip to content

Commit 0f77721

Browse files
Allowed useObjectTranslation to accept the same params as useTranslation (#845)
1 parent 762612f commit 0f77721

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/common/src/hooks/useObjectTranslation.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TranslationObject } from '@monkvision/types';
2-
import { useTranslation } from 'react-i18next';
2+
import { useTranslation, UseTranslationOptions } from 'react-i18next';
33
import { useCallback } from 'react';
44
import { getLanguage } from '../i18n';
55

@@ -17,8 +17,11 @@ export interface UseObjectTranslationResult {
1717
/**
1818
* Custom hook used to get a translation function tObj that translates TranslationObjects.
1919
*/
20-
export function useObjectTranslation(): UseObjectTranslationResult {
21-
const { i18n } = useTranslation();
20+
export function useObjectTranslation(
21+
ns?: string,
22+
options?: UseTranslationOptions<any>,
23+
): UseObjectTranslationResult {
24+
const { i18n } = useTranslation(ns, options);
2225
const tObj = useCallback(
2326
(obj: TranslationObject) => {
2427
return obj[getLanguage(i18n.language)] ?? 'translation-not-found';

packages/common/test/hooks/useObjectTranslation.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ describe('useObjectTranslation hook', () => {
2929
expect(typeof result.current.tObj({} as TranslationObject)).toBe('string');
3030
unmount();
3131
});
32+
33+
it('should pass the options parameters to the useTranslation hook', () => {
34+
const ns = 'test-ns';
35+
const options = { lng: 'nl' };
36+
const { unmount } = renderHook(() => useObjectTranslation(ns, options));
37+
expect(useTranslation).toHaveBeenCalledWith(ns, options);
38+
39+
unmount();
40+
});
3241
});

0 commit comments

Comments
 (0)