diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index 01ba6d2df6..fcf6553768 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -164,7 +164,7 @@ if (!count) { return callback(text); } - + if (S === null) { // browser environment and S not yet initialized // we need to wait for async require call stringDefer.then(function () { translateKeys(keys, text, language, callback); }); @@ -205,6 +205,10 @@ if (value) { var variable; for (var i = 1, ii = variables.length; i < ii; i++) { + + // see https://github.com/NodeBB/NodeBB/issues/1951 + variables[i] = variables[i].replace(/%/g, '%').replace(/,/g, ','); + variable = S(variables[i]).chompRight(']]').collapseWhitespace().escapeHTML().s; value = value.replace('%' + i, variable); } diff --git a/tests/translator.js b/tests/translator.js index 788fa3b31d..7546ec1d90 100644 --- a/tests/translator.js +++ b/tests/translator.js @@ -85,5 +85,15 @@ describe('Translator', function(){ }); }); + it('should properly escape % and ,', function(done) { + var title = 'Test 1, 2, 3 % salmon'; + title = title.replace(/%/g, '%').replace(/,/g, ','); + var key = "[[topic:composer.replying_to, " + title + "]]"; + translator.translate(key, function(translated) { + assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon'); + done(); + }); + }); + }); });