Skip to content

Commit

Permalink
nested interpolation with data model "replace", fixes #1896
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 3, 2023
1 parent c3e116b commit 7e91be4
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 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.8

- fix: nested interpolation with data model "replace"

## 22.4.7

- fix: interpolation with data model "replace"
Expand Down
3 changes: 2 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@
var nb = res.match(this.interpolator.nestingRegexp);
nestBef = nb && nb.length;
}
var data = options.replace && typeof options.replace !== 'string' && this.options.compatibilityAPI === 'v1' ? options.replace : options;
var data = options.replace && typeof options.replace !== 'string' ? options.replace : options;
if (this.options.interpolation.defaultVariables) data = _objectSpread$4(_objectSpread$4({}, this.options.interpolation.defaultVariables), data);
res = this.interpolator.interpolate(res, data, options.lng || this.language, options);
if (skipOnVariables) {
Expand Down Expand Up @@ -1554,6 +1554,7 @@
while (match = this.nestingRegexp.exec(str)) {
var formatters = [];
clonedOptions = _objectSpread$3({}, options);
clonedOptions = clonedOptions.replace && typeof clonedOptions.replace !== 'string' ? clonedOptions.replace : clonedOptions;
clonedOptions.applyPostProcessor = false;
delete clonedOptions.defaultValue;
var doReduce = false;
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Interpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ class Interpolator {
let formatters = [];

clonedOptions = { ...options };
clonedOptions =
clonedOptions.replace && typeof clonedOptions.replace !== 'string'
? clonedOptions.replace
: clonedOptions;
clonedOptions.applyPostProcessor = false; // avoid post processing on nested lookup
delete clonedOptions.defaultValue; // assert we do not get a endless loop on interpolating defaultValue again and again

Expand Down
7 changes: 1 addition & 6 deletions src/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,7 @@ class Translator extends EventEmitter {
}

// interpolate
let data =
options.replace &&
typeof options.replace !== 'string' &&
this.options.compatibilityAPI === 'v1'
? options.replace
: options;
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, options);
Expand Down

0 comments on commit 7e91be4

Please sign in to comment.