From baaad1328696167b1fce022b2ee52081b4cf852a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 27 Jan 2016 09:13:57 -0500 Subject: [PATCH] reverted latest change to translator match regex - Fixes issue with parentheses in translations (closes #4107) - No longer marking translation keys invalid if they contain HTML, as that was probably not performant. Instead, parameters will simply be escaped via the StringJS library. --- public/src/modules/translator.js | 12 ++++++++++-- src/meta/templates.js | 1 - tests/translator.js | 9 ++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/public/src/modules/translator.js b/public/src/modules/translator.js index 0824cce59e..6c3db46116 100644 --- a/public/src/modules/translator.js +++ b/public/src/modules/translator.js @@ -2,14 +2,21 @@ "use strict"; /* globals RELATIVE_PATH, config, define */ + var S; + // export the class if we are in a Node-like system. if (typeof module === 'object' && module.exports === translator) { exports = module.exports = translator; + S = require('string'); + } else { + require(['string'], function(stringLib) { + S = stringLib; + }); } var languages = {}, regexes = { - match: /\[\[\w+:[\w\.]+((?!\[\[|<|>|\(|\)).)*?\]\]/g, // see tests/translator.js for an explanation re: this monster + match: /\[\[\w+:[\w\.]+((?!\[\[).)*?\]\]/g, // see tests/translator.js for an explanation re: this monster split: /[,][\s]*/, replace: /\]+$/ }; @@ -187,8 +194,9 @@ function insertLanguage(text, key, value, variables) { if (value) { + var variable; for (var i = 1, ii = variables.length; i < ii; i++) { - var variable = variables[i].replace(']]', ''); + variable = S(variables[i]).chompRight(']]').collapseWhitespace().escapeHTML().s; value = value.replace('%' + i, variable); } diff --git a/src/meta/templates.js b/src/meta/templates.js index cc6f6ad2cd..9a5aa5c72b 100644 --- a/src/meta/templates.js +++ b/src/meta/templates.js @@ -76,7 +76,6 @@ Templates.compile = function(callback) { baseTpls.forEach(function(el, i) { paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]); }); -// console.log(pluginTemplates); for (var tpl in pluginTemplates) { if (pluginTemplates.hasOwnProperty(tpl)) { diff --git a/tests/translator.js b/tests/translator.js index c3f01b4d15..ede6acd75a 100644 --- a/tests/translator.js +++ b/tests/translator.js @@ -71,10 +71,17 @@ describe('Translator', function(){ }); }); + it('should properly handle parameters that contain parentheses', function(done) { + translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]', function(translated) { + assert.strictEqual(translated, '(foobar) out of Home'); + done(); + }); + }); + it('should not translate language key parameters with HTML in them', function(done) { var key = '[[global:403.login, test]]'; translator.translate(key, function(translated) { - assert.strictEqual(translated, key); + assert.strictEqual(translated, 'Perhaps you should try logging in?'); done(); }); })