Skip to content

Commit

Permalink
add key prefix support to useTranslation hook (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Sep 8, 2021
1 parent ca410ff commit 0404f39
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function useSSR(initialI18nStore: Resource, initialLanguage: string): voi
export interface UseTranslationOptions {
i18n?: i18n;
useSuspense?: boolean;
keyPrefix?: string;
}
export type UseTranslationResponse = [TFunction, i18n, boolean] & {
t: TFunction;
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"eslint-plugin-react": "^7.16.0",
"eslint-plugin-testing-library": "^3.10.1",
"husky": "^3.0.3",
"i18next": "^20.0.0",
"i18next": "^20.6.0",
"jest": "^24.8.0",
"jest-cli": "^24.8.4",
"lint-staged": "^8.1.3",
Expand Down
8 changes: 6 additions & 2 deletions src/useTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useTranslation(ns, props = {}) {
);

const i18nOptions = { ...getDefaults(), ...i18n.options.react, ...props };
const { useSuspense } = i18nOptions;
const { useSuspense, keyPrefix } = i18nOptions;

// prepare having a namespace
let namespaces = ns || defaultNSFromContext || (i18n.options && i18n.options.defaultNS);
Expand All @@ -40,7 +40,11 @@ export function useTranslation(ns, props = {}) {

// binding t function to namespace (acts also as rerender trigger)
function getT() {
return i18n.getFixedT(null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0]);
return i18n.getFixedT(
null,
i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0],
keyPrefix,
);
}
const [t, setT] = useState(getT);

Expand Down
13 changes: 13 additions & 0 deletions test/useTranslation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ describe('useTranslation', () => {
});
});

describe('key prefix', () => {
i18nInstance.addResource('en', 'translation', 'deeply.nested.key', 'here!');

it('should apply keyPrefix', () => {
const { result } = renderHook(() =>
useTranslation('translation', { i18n: i18nInstance, keyPrefix: 'deeply.nested' }),
);
const { t } = result.current;
expect(t('key')).toBe('here!');
expect(t.keyPrefix).toBe('deeply.nested');
});
});

describe('replacing i18n instance in provider', () => {
i18nInstance.addResource('fr', 'translation', 'key1', 'test2');
const i18nInstanceClone = i18nInstance.cloneInstance({ lng: 'fr' });
Expand Down
1 change: 1 addition & 0 deletions ts4.1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export function useSSR(initialI18nStore: Resource, initialLanguage: string): voi
export interface UseTranslationOptions {
i18n?: i18n;
useSuspense?: boolean;
keyPrefix?: string;
}

type UseTranslationResponse<N extends Namespace> = [TFunction<N>, i18n, boolean] & {
Expand Down

0 comments on commit 0404f39

Please sign in to comment.