Skip to content

Commit

Permalink
Feature: key prefix option (#1644)
Browse files Browse the repository at this point in the history
* add keyPrefix argument to getFixedT

* add typescript types

* add unit tests

* fix formatting

* improve tests

* code review changes: ts signature
  • Loading branch information
mdjastrzebski committed Sep 1, 2021
1 parent e8566a0 commit e3e9381
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,9 +947,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

0 comments on commit e3e9381

Please sign in to comment.