Skip to content

Commit

Permalink
optimize single quotes replacement for $t() nesting #1836
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Sep 18, 2022
1 parent 132a024 commit c898420
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 21.9.2

- optimize single quotes replacement for $t() nesting [1836](https://github.com/i18next/i18next/issues/1836)

## 21.9.1

- defaultNS can now also be set to false
Expand Down
19 changes: 18 additions & 1 deletion i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@
prefix: "".concat(this.prefix, ":").concat(moduleName, ":")
}), this.options));
}
}, {
key: "clone",
value: function clone(options) {
options = options || this.options;
options.prefix = options.prefix || this.prefix;
return new Logger(this.logger, options);
}
}]);

return Logger;
Expand Down Expand Up @@ -1747,7 +1754,12 @@
var optionsString = "{".concat(c[1]);
key = c[0];
optionsString = this.interpolate(optionsString, clonedOptions);
optionsString = optionsString.replace(/'/g, '"');
var matchedSingleQuotes = optionsString.match(/'/g);
var matchedDoubleQuotes = optionsString.match(/"/g);

if (matchedSingleQuotes && matchedSingleQuotes.length % 2 === 0 && !matchedDoubleQuotes || matchedDoubleQuotes.length % 2 !== 0) {
optionsString = optionsString.replace(/'/g, '"');
}

try {
clonedOptions = JSON.parse(optionsString);
Expand Down Expand Up @@ -2872,6 +2884,11 @@
});

var clone = new I18n(mergedOptions);

if (options.debug !== undefined || options.prefix !== undefined) {
clone.logger = clone.logger.clone(options);
}

var membersToCopy = ['store', 'services', 'language'];
membersToCopy.forEach(function (m) {
clone[m] = _this8[m];
Expand Down
2 changes: 1 addition & 1 deletion i18next.min.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/Interpolator.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ class Interpolator {
let optionsString = `{${c[1]}`;
key = c[0];
optionsString = this.interpolate(optionsString, clonedOptions);
optionsString = optionsString.replace(/'/g, '"');
const matchedSingleQuotes = optionsString.match(/'/g);
const matchedDoubleQuotes = optionsString.match(/"/g);
if (
(matchedSingleQuotes && matchedSingleQuotes.length % 2 === 0 && !matchedDoubleQuotes) ||
matchedDoubleQuotes.length % 2 !== 0
) {
optionsString = optionsString.replace(/'/g, '"');
}

try {
clonedOptions = JSON.parse(optionsString);
Expand Down
16 changes: 16 additions & 0 deletions test/i18next.interpolation.nesting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('i18next.interpolation.nesting', () => {
test_foo: "foo $t(test, { 'context': 'bar' })",
test_bar: "bar $t(test, { 'context': 'baz' })",
test_baz: 'baz',
foo: '$t(bar, {"firstName": "{{firstName}}", "lastName": "{{lastName}}" })',
bar: '{{firstName}} {{lastName}}',
},
},
},
Expand Down Expand Up @@ -91,6 +93,20 @@ describe('i18next.interpolation.nesting', () => {
args: ['test', { context: 'foo' }],
expected: 'foo bar baz',
},
{
args: [
'foo',
{ firstName: `Cool`, lastName: `O'Dood`, interpolation: { escapeValue: false } },
],
expected: `Cool O'Dood`,
},
{
args: [
'bar',
{ firstName: `Cool`, lastName: `O'Dood`, interpolation: { escapeValue: false } },
],
expected: `Cool O'Dood`,
},
];

tests.forEach((test) => {
Expand Down

0 comments on commit c898420

Please sign in to comment.