Skip to content

Commit

Permalink
Extend skipOnVariables (#1488)
Browse files Browse the repository at this point in the history
* extend usage of skipOnVariables, should fix #1487
  • Loading branch information
adrai committed Jul 15, 2020
1 parent 8c63160 commit c10ee50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
83 changes: 39 additions & 44 deletions src/Interpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,53 +99,48 @@ class Interpolator {
const missingInterpolationHandler =
(options && options.missingInterpolationHandler) || this.options.missingInterpolationHandler;

replaces = 0;
// unescape if has unescapePrefix/Suffix
/* eslint no-cond-assign: 0 */
while ((match = this.regexpUnescape.exec(str))) {
value = handleFormat(match[1].trim());
if (value === undefined) {
if (typeof missingInterpolationHandler === 'function') {
const temp = missingInterpolationHandler(str, match, options);
value = typeof temp === 'string' ? temp : '';
} else {
this.logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
value = '';
const skipOnVariables =
(options && options.interpolation && options.interpolation.skipOnVariables) ||
this.options.interpolation.skipOnVariables;

const todos = [
{
// unescape if has unescapePrefix/Suffix
regex: this.regexpUnescape,
safeValue: val => regexSafe(val),
},
{
// regular escape on demand
regex: this.regexp,
safeValue: val => (this.escapeValue ? regexSafe(this.escape(val)) : regexSafe(val)),
},
];
todos.forEach(todo => {
replaces = 0;
/* eslint no-cond-assign: 0 */
while ((match = todo.regex.exec(str))) {
value = handleFormat(match[1].trim());
if (value === undefined) {
if (typeof missingInterpolationHandler === 'function') {
const temp = missingInterpolationHandler(str, match, options);
value = typeof temp === 'string' ? temp : '';
} else if (skipOnVariables) {
value = match[0];
} else {
this.logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
value = '';
}
} else if (typeof value !== 'string' && !this.useRawValueToEscape) {
value = utils.makeString(value);
}
} else if (typeof value !== 'string' && !this.useRawValueToEscape) {
value = utils.makeString(value);
}
str = str.replace(match[0], regexSafe(value));
this.regexpUnescape.lastIndex = 0;
replaces++;
if (replaces >= this.maxReplaces) {
break;
}
}

replaces = 0;
// regular escape on demand
while ((match = this.regexp.exec(str))) {
value = handleFormat(match[1].trim());
if (value === undefined) {
if (typeof missingInterpolationHandler === 'function') {
const temp = missingInterpolationHandler(str, match, options);
value = typeof temp === 'string' ? temp : '';
} else {
this.logger.warn(`missed to pass in variable ${match[1]} for interpolating ${str}`);
value = '';
str = str.replace(match[0], todo.safeValue(value));
todo.regex.lastIndex = 0;
replaces++;
if (replaces >= this.maxReplaces) {
break;
}
} else if (typeof value !== 'string' && !this.useRawValueToEscape) {
value = utils.makeString(value);
}
value = this.escapeValue ? regexSafe(this.escape(value)) : regexSafe(value);
str = str.replace(match[0], value);
this.regexp.lastIndex = 0;
replaces++;
if (replaces >= this.maxReplaces) {
break;
}
}
});
return str;
}

Expand Down
4 changes: 4 additions & 0 deletions test/i18next.interpolation.skipOnVariables.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ describe('i18next.interpolation.nesting', () => {
args: ['keyWithNestAndVar', { a: '$t(nested)' }],
expected: '$t(nested2) value $t(nested)',
},
{
args: ['key', { a: '{{nested}}' }],
expected: 'value {{nested}}',
},
];

tests.forEach(test => {
Expand Down

0 comments on commit c10ee50

Please sign in to comment.