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.
v1.18.x
Julian Lam 10 years ago
parent 60c86400d7
commit baaad13286

@ -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);
}

@ -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)) {

@ -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, <strong>test</strong>]]';
translator.translate(key, function(translated) {
assert.strictEqual(translated, key);
assert.strictEqual(translated, 'Perhaps you should <a href=\'&lt;strong&gt;test&lt;/strong&gt;/login\'>try logging in</a>?');
done();
});
})

Loading…
Cancel
Save