From f1f00b63fb39d52f1912cef35d4b8a1c317ea9d8 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 27 Jan 2017 20:35:50 +0300 Subject: [PATCH] closes #5400 --- public/src/modules/translator.js | 6 +++--- test/translator.js | 17 ++++++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index eac467f413..db3953229e 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -155,7 +155,7 @@ while (cursor + 2 <= len) { sliced = str.slice(cursor, cursor + 2); - // found some text after the double bracket, + // found some text after the double bracket, // so this is probably a translation string if (!textBeforeColonFound && validTextRegex.test(sliced[0])) { textBeforeColonFound = true; @@ -174,7 +174,7 @@ cursor += 1; // a space or comma was found before the name // this isn't a translation string, so back out - } else if (!(textBeforeColonFound && colonFound && textAfterColonFound && commaAfterNameFound) && + } else if (!(textBeforeColonFound && colonFound && textAfterColonFound && commaAfterNameFound) && invalidTextRegex.test(sliced[0])) { cursor += 1; lastBreak -= 2; @@ -272,7 +272,7 @@ } var out = translated; translatedArgs.forEach(function (arg, i) { - var escaped = arg.replace(/%/g, '%').replace(/\\,/g, ','); + var escaped = arg.replace(/%(?=\d)/g, '%').replace(/\\,/g, ','); out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped); }); return out; diff --git a/test/translator.js b/test/translator.js index 8198814164..17affe1700 100644 --- a/test/translator.js +++ b/test/translator.js @@ -4,6 +4,7 @@ var assert = require('assert'); var shim = require('../public/src/modules/translator.js'); var Translator = shim.Translator; +var db = require('./mocks/databasemock'); require('../src/languages').init(function () {}); @@ -118,10 +119,20 @@ describe('new Translator(language)', function () { it('should properly escape and ignore % and \\, in arguments', function () { var translator = Translator.create('en-GB'); - var title = 'Test 1\\, 2\\, 3 % salmon'; + var title = 'Test 1\\, 2\\, 3 %2 salmon'; var key = "[[topic:composer.replying_to, " + title + "]]"; return 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 %2 salmon'); + }); + }); + + it('should not escape regular %', function () { + var translator = Translator.create('en-GB'); + + var title = '3 % salmon'; + var key = "[[topic:composer.replying_to, " + title + "]]"; + return translator.translate(key).then(function (translated) { + assert.strictEqual(translated, 'Replying to 3 % salmon'); }); }); @@ -173,7 +184,7 @@ describe('Translator.create()', function () { describe('Translator modules', function () { it('should work before registered', function () { var translator = Translator.create(); - + Translator.registerModule('test-custom-integer-format', function (lang) { return function (key, args) { var num = parseInt(args[0], 10) || 0;