Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add key prefix support to useTranslation hook #1371

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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