Escape and ignore `%` and `\,` in translator args

v1.18.x
Peter Jaszkowiak 8 years ago
parent 1c6cee207f
commit 7c697759e9

@ -105,7 +105,7 @@
} else if (text.slice(i, i + 2) === ']]') {
level -= 1;
i += 1;
} else if (level === 0 && text[i] === ',') {
} else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') {
arr.push(text.slice(brk, i).trim());
i += 1;
brk = i;
@ -260,7 +260,8 @@
}
var out = translated;
translatedArgs.forEach(function (arg, i) {
out = out.replace(new RegExp('%' + (i + 1), 'g'), arg);
var escaped = arg.replace(/%/g, '%').replace(/\\,/g, ',');
out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped);
});
return out;
});

@ -127,14 +127,13 @@ describe('new Translator(language)', function () {
});
});
it('should properly escape % and ,', function (done) {
it('should properly escape and ignore % and \\, in arguments', function (done) {
var translator = Translator.create('en-GB');
var title = 'Test 1, 2, 3 % salmon';
title = title.replace(/%/g, '%').replace(/,/g, ',');
var title = 'Test 1\\, 2\\, 3 % salmon';
var key = "[[topic:composer.replying_to, " + title + "]]";
translator.translate(key).then(function (translated) {
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
done();
});
});

Loading…
Cancel
Save