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

1618 - Add 'ns' to the 'returnedObjectHandler' options #1619

Merged
merged 2 commits into from May 28, 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
2 changes: 1 addition & 1 deletion src/Translator.js
Expand Up @@ -133,7 +133,7 @@ class Translator extends EventEmitter {
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)
? this.options.returnedObjectHandler(resUsedKey, res, { ...options, ns: namespaces })
: `key '${key} (${this.language})' returned an object instead of string.`;
}

Expand Down
83 changes: 83 additions & 0 deletions test/translator/translator.translate.returnObject.spec.js
Expand Up @@ -93,4 +93,87 @@ describe('Translator', () => {
});
});
});
describe('translate() with returnObjects=false', () => {
let t;
let rs;
let lu;
before(() => {
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'],
},
},
});
lu = new LanguageUtils({ fallbackLng: 'en' });

t = new Translator(
{
resourceStore: rs,
languageUtils: lu,
pluralResolver: new PluralResolver(lu, { prepend: '_', simplifyPluralSuffix: true }),
interpolator: new Interpolator(),
},
{
keySeparator: '.',
contextSeparator: '_',
returnObjects: false,
returnedObjectHandler: (...args) => args,
ns: ['common', 'special'],
defaultNS: 'common',
interpolation: {},
},
);

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),
);
});
});
});
});
});