Skip to content

Commit

Permalink
Allowed useObjectTranslation to accept the same params as useTranslat…
Browse files Browse the repository at this point in the history
…ion (#845)
  • Loading branch information
souyahia-monk authored Sep 12, 2024
1 parent 762612f commit 0f77721
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/common/src/hooks/useObjectTranslation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TranslationObject } from '@monkvision/types';
import { useTranslation } from 'react-i18next';
import { useTranslation, UseTranslationOptions } from 'react-i18next';
import { useCallback } from 'react';
import { getLanguage } from '../i18n';

Expand All @@ -17,8 +17,11 @@ export interface UseObjectTranslationResult {
/**
* Custom hook used to get a translation function tObj that translates TranslationObjects.
*/
export function useObjectTranslation(): UseObjectTranslationResult {
const { i18n } = useTranslation();
export function useObjectTranslation(
ns?: string,
options?: UseTranslationOptions<any>,
): UseObjectTranslationResult {
const { i18n } = useTranslation(ns, options);
const tObj = useCallback(
(obj: TranslationObject) => {
return obj[getLanguage(i18n.language)] ?? 'translation-not-found';
Expand Down
9 changes: 9 additions & 0 deletions packages/common/test/hooks/useObjectTranslation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ describe('useObjectTranslation hook', () => {
expect(typeof result.current.tObj({} as TranslationObject)).toBe('string');
unmount();
});

it('should pass the options parameters to the useTranslation hook', () => {
const ns = 'test-ns';
const options = { lng: 'nl' };
const { unmount } = renderHook(() => useObjectTranslation(ns, options));
expect(useTranslation).toHaveBeenCalledWith(ns, options);

unmount();
});
});

0 comments on commit 0f77721

Please sign in to comment.