Skip to content

Commit

Permalink
fix: getFixedT with keyPrefix and fallback keys usage #1604
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 8, 2023
1 parent 5bd9ac0 commit dbe9940
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 22.4.9

- fix: getFixedT with keyPrefix and fallback keys usage [1604](https://github.com/i18next/i18next/issues/1604)

## 22.4.8

- fix: nested interpolation with data model "replace"
Expand Down
9 changes: 8 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -2444,7 +2444,14 @@
options.ns = options.ns || fixedT.ns;
options.keyPrefix = options.keyPrefix || keyPrefix || fixedT.keyPrefix;
var keySeparator = _this5.options.keySeparator || '.';
var resultKey = options.keyPrefix ? "".concat(options.keyPrefix).concat(keySeparator).concat(key) : key;
var resultKey;
if (options.keyPrefix && Array.isArray(key)) {
resultKey = key.map(function (k) {
return "".concat(options.keyPrefix).concat(keySeparator).concat(k);
});
} else {
resultKey = options.keyPrefix ? "".concat(options.keyPrefix).concat(keySeparator).concat(key) : key;
}
return _this5.t(resultKey, options);
};
if (typeof lng === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ class I18n extends EventEmitter {
options.keyPrefix = options.keyPrefix || keyPrefix || fixedT.keyPrefix;

const keySeparator = this.options.keySeparator || '.';
const resultKey = options.keyPrefix ? `${options.keyPrefix}${keySeparator}${key}` : key;
let resultKey
if (options.keyPrefix && Array.isArray(key)) {
resultKey = key.map(k => `${options.keyPrefix}${keySeparator}${k}`);
} else {
resultKey = options.keyPrefix ? `${options.keyPrefix}${keySeparator}${key}` : key;
}
return this.t(resultKey, options);
};
if (typeof lng === 'string') {
Expand Down
11 changes: 11 additions & 0 deletions test/i18next.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ describe('i18next', () => {
expect(t('nested.key', { keyPrefix: 'deeply' })).to.equal('quì!');
expect(t.keyPrefix).to.equal('deeply.nested');
});
it('should apply keyPrefix also for fallback keys', () => {
i18next.addResource('fr', 'translation', 'group.key1', 'Translation 1');
i18next.addResource('fr', 'translation', 'group.key2', 'Translation 2');
const t = i18next.getFixedT('fr', null, 'group');
expect(t('key1')).to.equal('Translation 1');
expect(t.keyPrefix).to.equal('group');
expect(t('key2')).to.equal('Translation 2');
expect(t.keyPrefix).to.equal('group');
expect(i18next.t(['group.key1', 'group.key2'], { lng: 'fr' })).to.equal('Translation 1');
expect(t(['key1', 'key2'])).to.equal('Translation 1');
});
});
});

Expand Down

0 comments on commit dbe9940

Please sign in to comment.