Skip to content

Commit

Permalink
Allow missingInterpolationHandler to be provided as t() option
Browse files Browse the repository at this point in the history
  • Loading branch information
mogelbrod committed Oct 18, 2018
1 parent 0de7e56 commit 1fc55ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions src/Interpolator.js
Expand Up @@ -55,7 +55,7 @@ class Interpolator {
this.nestingRegexp = new RegExp(nestingRegexpStr, 'g');
}

interpolate(str, data, lng) {
interpolate(str, data, lng, options) {
let match;
let value;
let replaces;
Expand All @@ -76,6 +76,9 @@ class Interpolator {

this.resetRegExp();

const missingInterpolationHandler = (options && options.missingInterpolationHandler) ||
this.options.missingInterpolationHandler;

replaces = 0;
// unescape if has unescapePrefix/Suffix
/* eslint no-cond-assign: 0 */
Expand All @@ -94,8 +97,8 @@ class Interpolator {
while (match = this.regexp.exec(str)) {
value = handleFormat(match[1].trim());
if (value === undefined) {
if (typeof this.options.missingInterpolationHandler === 'function') {
const temp = this.options.missingInterpolationHandler(str, match);
if (typeof missingInterpolationHandler === 'function') {
const temp = missingInterpolationHandler(str, match);
value = typeof temp === 'string' ? temp : '';
} else {
this.logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
Expand Down
2 changes: 1 addition & 1 deletion src/Translator.js
Expand Up @@ -197,7 +197,7 @@ class Translator extends EventEmitter {
// interpolate
let data = options.replace && typeof options.replace !== 'string' ? options.replace : options;
if (this.options.interpolation.defaultVariables) data = { ...this.options.interpolation.defaultVariables, ...data };
res = this.interpolator.interpolate(res, data, options.lng || this.language);
res = this.interpolator.interpolate(res, data, options.lng || this.language, options);

// nesting
if (options.nest !== false) res = this.interpolator.nest(res, (...args) => this.translate(...args), options);
Expand Down
10 changes: 8 additions & 2 deletions test/interpolation.spec.js
Expand Up @@ -269,7 +269,7 @@ describe('Interpolator', () => {
});

tests.forEach((test) => {
it('correctly calls missingInterpolationHandler for ' + JSON.stringify(test.args) + ' args', () => {
it('correctly calls missingInterpolationHandler for ' + JSON.stringify(test.args) + ' args', () => {
expect(ip.interpolate.apply(ip, test.args)).to.eql(test.expected);
});
});
Expand All @@ -293,10 +293,16 @@ describe('Interpolator', () => {
});

tests.forEach((test) => {
it('correctly calls missingInterpolationHandler for ' + JSON.stringify(test.args) + ' args', () => {
it('correctly calls missingInterpolationHandler for ' + JSON.stringify(test.args) + ' args', () => {
expect(ip.interpolate.apply(ip, test.args)).to.eql(test.expected);
});
});

it('correctly calls handler provided via options', () => {
expect(ip.interpolate('{{custom}}', {}, null, {
missingInterpolationHandler: (str, match) => 'overridden',
})).to.eql('overridden');
});
});

describe("interpolate() - with null interpolation value - not filled by missingInterpolationHandler", () => {
Expand Down

0 comments on commit 1fc55ff

Please sign in to comment.