mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-06-25 01:09:36 +02:00
Fixed #4033
Also related to regression first caused by the fix to #3695 from
fcb381f922
Also, added tests for translator. omg.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
"back_to_home": "Back to %1",
|
||||
"outgoing_link": "Outgoing Link",
|
||||
"outgoing_link_message": "You are now leaving %1.",
|
||||
"outgoing_link_message": "You are now leaving %1",
|
||||
"continue_to": "Continue to %1",
|
||||
"return_to": "Return to %1",
|
||||
"new_notification": "New Notification",
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
var languages = {},
|
||||
regexes = {
|
||||
match: /\[\[\w+:[^\[]*?\]\]/g,
|
||||
match: /\[\[\w+:((?!\[\[).)*?\]\]/g,
|
||||
split: /[,][\s]*/,
|
||||
replace: /\]+$/
|
||||
};
|
||||
@@ -138,7 +138,14 @@
|
||||
return callback(text);
|
||||
}
|
||||
|
||||
translateKeys(keys, text, language, callback);
|
||||
translateKeys(keys, text, language, function(translated) {
|
||||
keys = translated.match(regexes.match);
|
||||
if (!keys) {
|
||||
callback(translated);
|
||||
} else {
|
||||
translateKeys(keys, translated, language, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function translateKeys(keys, text, language, callback) {
|
||||
@@ -153,12 +160,7 @@
|
||||
translateKey(key, data, language, function(translated) {
|
||||
--count;
|
||||
if (count <= 0) {
|
||||
keys = translated.text.match(regexes.match);
|
||||
if (!keys) {
|
||||
callback(translated.text);
|
||||
} else {
|
||||
translateKeys(keys, translated.text, language, callback);
|
||||
}
|
||||
callback(translated.text);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
67
tests/translator.js
Normal file
67
tests/translator.js
Normal file
@@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
/*global require*/
|
||||
|
||||
var assert = require('assert'),
|
||||
db = require('./mocks/databasemock'),
|
||||
translator = require('../public/src/modules/translator.js');
|
||||
|
||||
|
||||
describe('Translator', function(){
|
||||
describe('.translate()', function(){
|
||||
it('should handle basic translations', function(done) {
|
||||
translator.translate('[[global:home]]', function(translated) {
|
||||
assert.strictEqual(translated, 'Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys in regular text', function(done) {
|
||||
translator.translate('Let\'s go [[global:home]]', function(translated) {
|
||||
assert.strictEqual(translated, 'Let\'s go Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should accept a language parameter and adjust accordingly', function(done) {
|
||||
translator.translate('[[global:home]]', 'de', function(translated) {
|
||||
assert.strictEqual(translated, 'Übersicht');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys in regular text with another language specified', function(done) {
|
||||
translator.translate('[[global:home]] test', 'de', function(translated) {
|
||||
assert.strictEqual(translated, 'Übersicht test');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys with parameters', function(done) {
|
||||
translator.translate('[[global:pagination.out_of, 1, 5]]', function(translated) {
|
||||
assert.strictEqual(translated, '1 out of 5');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys inside language keys', function(done) {
|
||||
translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]', function(translated) {
|
||||
assert.strictEqual(translated, 'You are now leaving Guest');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys inside language keys with multiple parameters', function(done) {
|
||||
translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]', function(translated) {
|
||||
assert.strictEqual(translated, '<strong>Guest</strong> has posted a reply to: <strong>My Topic</strong>');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should properly handle parameters that contain square brackets', function(done) {
|
||||
translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]', function(translated) {
|
||||
assert.strictEqual(translated, '[guest] out of Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user