Skip to content

Commit

Permalink
locally merge #1617 to fix #1614
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed May 28, 2021
1 parent 20f9ce2 commit 4abde3f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 20.3.1

- add 'ns' to the 'returnedObjectHandler' options [1619](https://github.com/i18next/i18next/pull/1619)
- disable warn 'accessing an object' when the 'returnedObjectHandler' is defined [1617](https://github.com/i18next/i18next/pull/1617)

### 20.3.0

- add simple toJSON function to fix uncontrolled serialization, fixes [1322](https://github.com/i18next/react-i18next/issues/1322)
Expand Down
9 changes: 7 additions & 2 deletions i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,13 @@

if (handleAsObjectInI18nFormat && res && handleAsObject && noObject.indexOf(resType) < 0 && !(typeof joinArrays === 'string' && resType === '[object Array]')) {
if (!options.returnObjects && !this.options.returnObjects) {
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
return this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, res, options) : "key '".concat(key, " (").concat(this.language, ")' returned an object instead of string.");
if (!this.options.returnedObjectHandler) {
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
}

return this.options.returnedObjectHandler ? this.options.returnedObjectHandler(resUsedKey, res, _objectSpread({}, options, {
ns: namespaces
})) : "key '".concat(key, " (").concat(this.language, ")' returned an object instead of string.");
}

if (keySeparator) {
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ class Translator extends EventEmitter {
!(typeof joinArrays === 'string' && resType === '[object Array]')
) {
if (!options.returnObjects && !this.options.returnObjects) {
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
if (!this.options.returnedObjectHandler) {
this.logger.warn('accessing an object - but returnObjects options is not enabled!');
}
return this.options.returnedObjectHandler
? this.options.returnedObjectHandler(resUsedKey, res, { ...options, ns: namespaces })
: `key '${key} (${this.language})' returned an object instead of string.`;
Expand Down
89 changes: 45 additions & 44 deletions test/translator/translator.translate.returnObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ResourceStore from '../../src/ResourceStore.js';
import LanguageUtils from '../../src/LanguageUtils';
import PluralResolver from '../../src/PluralResolver';
import Interpolator from '../../src/Interpolator';
import logger from '../../src/logger';

describe('Translator', () => {
describe('translate() with returnObjects=true', () => {
Expand Down Expand Up @@ -93,22 +94,24 @@ describe('Translator', () => {
});
});
});

describe('translate() with returnObjects=false', () => {
let t;
let loggerStub;
let rs;
let lu;
const loggerSpy = sinon.spy();
before(() => {
loggerStub = sinon.stub(logger, 'create');
loggerStub.returns({
warn: loggerSpy,
log: sinon.spy(),
});

rs = new ResourceStore({
en: {
common: {
array: ['common_array_en_1', 'common_array_en_2'],
object: {
value: 'common_object_en_value',
},
array_with_context: ['lorem ipsum', 'lorem ipsum', 'hello {{what}}'],
},
special: {
array: ['special_array_en_1', 'special_array_en_2'],
},
},
});
Expand All @@ -135,44 +138,42 @@ describe('Translator', () => {
t.changeLanguage('en');
});

var tests = [
{
args: ['common:array'],
expected: ['array', ['common_array_en_1', 'common_array_en_2'], { ns: ['common'] }],
},
{
args: ['array'],
expected: ['array', ['common_array_en_1', 'common_array_en_2'], { ns: ['common'] }],
},
{
args: ['common:array_with_context', { what: 'world' }],
expected: [
'array_with_context',
['lorem ipsum', 'lorem ipsum', 'hello {{what}}'],
{ what: 'world', ns: ['common'] },
],
},
{
args: ['common:object', { what: 'world' }],
expected: [
'object',
{ value: 'common_object_en_value' },
{ what: 'world', ns: ['common'] },
],
},
{
args: ['special:array'],
expected: ['array', ['special_array_en_1', 'special_array_en_2'], { ns: ['special'] }],
},
];

describe('and "returnedObjectHandler" defined', () => {
tests.forEach(test => {
it('correctly translates for ' + JSON.stringify(test.args) + ' args', () => {
expect(JSON.stringify(t.translate.apply(t, test.args))).to.eql(
JSON.stringify(test.expected),
);
});
afterEach(() => {
loggerStub.reset();
});

it('should not emit a warning', () => {
t.translate.apply(t, ['common:array']);
expect(
loggerSpy.calledWith('accessing an object - but returnObjects options is not enabled!'),
).to.be.false;
});
});

describe('and "returnedObjectHandler" is not defined', () => {
it('should emit a warning', () => {
t = new Translator(
{
resourceStore: rs,
languageUtils: lu,
pluralResolver: new PluralResolver(lu, { prepend: '_', simplifyPluralSuffix: true }),
interpolator: new Interpolator(),
},
{
keySeparator: '.',
contextSeparator: '_',
returnObjects: false,
ns: ['common', 'special'],
defaultNS: 'common',
interpolation: {},
},
);
t.changeLanguage('en');
t.translate.apply(t, ['common:array']);
expect(
loggerSpy.calledWith('accessing an object - but returnObjects options is not enabled!'),
).to.be.true;
});
});
});
Expand Down

0 comments on commit 4abde3f

Please sign in to comment.