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: key prefix option #1644

Merged
merged 6 commits into from
Sep 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -940,9 +940,15 @@ export interface i18n {
* Returns a t function that defaults to given language or namespace.
* Both params could be arrays of languages or namespaces and will be treated as fallbacks in that case.
* On the returned function you can like in the t function override the languages or namespaces by passing them in options or by prepending namespace.
*
* Accepts optional keyPrefix that will be automatically applied to returned t function.
*/
getFixedT(lng: string | readonly string[], ns?: string | readonly string[]): TFunction;
getFixedT(lng: null, ns: string | readonly string[]): TFunction;
getFixedT(
lng: string | readonly string[],
ns?: string | readonly string[],
keyPrefix?: string,
): TFunction;
getFixedT(lng: null, ns: string | readonly string[] | null, keyPrefix?: string): TFunction;

/**
* Changes the language. The callback will be called as soon translations were loaded or an error occurs while loading.
Expand Down
8 changes: 6 additions & 2 deletions src/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class I18n extends EventEmitter {
return deferred;
}

getFixedT(lng, ns) {
getFixedT(lng, ns, keyPrefix) {
const fixedT = (key, opts, ...rest) => {
let options;
if (typeof opts !== 'object') {
Expand All @@ -322,14 +322,18 @@ class I18n extends EventEmitter {
options.lng = options.lng || fixedT.lng;
options.lngs = options.lngs || fixedT.lngs;
options.ns = options.ns || fixedT.ns;
return this.t(key, options);

const keySeparator = this.options.keySeparator || '.';
const resultKey = keyPrefix ? `${keyPrefix}${keySeparator}${key}` : key;
return this.t(resultKey, options);
};
if (typeof lng === 'string') {
fixedT.lng = lng;
} else {
fixedT.lngs = lng;
}
fixedT.ns = ns;
fixedT.keyPrefix = keyPrefix;
return fixedT;
}

Expand Down
6 changes: 6 additions & 0 deletions test/i18next.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ describe('i18next', () => {
expect(translatedKey).to.equal('default');
expect(translatedSecondKey).to.equal('default');
});
it('should apply keyPrefix', () => {
i18next.addResource('fr', 'translation', 'deeply.nested.key', 'ici!');
const t = i18next.getFixedT('fr', null, 'deeply.nested');
expect(t('key')).to.equal('ici!');
expect(t.keyPrefix).to.equal('deeply.nested');
});
});
});

Expand Down