mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-06 12:31:33 +01:00
Merge branch 'translator-escapes' of https://github.com/pitaj/NodeBB into pitaj-translator-escapes
This commit is contained in:
123
nodebb.bat
123
nodebb.bat
@@ -1,122 +1 @@
|
||||
@echo off
|
||||
|
||||
rem %1 action
|
||||
rem %2 subaction
|
||||
|
||||
setlocal enabledelayedexpansion
|
||||
2>nul call :CASE_%1
|
||||
if ERRORLEVEL 1 call :DEFAULT_CASE
|
||||
|
||||
exit /B
|
||||
|
||||
:CASE_start
|
||||
echo Starting NodeBB
|
||||
echo "nodebb.bat stop" to stop the NodeBB server
|
||||
echo "nodebb.bat log" to view server output
|
||||
|
||||
rem Start the loader daemon
|
||||
node loader %*
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_stop
|
||||
call :pidexists
|
||||
if %_result%==0 (
|
||||
echo NodeBB is already stopped.
|
||||
) else (
|
||||
echo Stopping NodeBB. Goodbye!
|
||||
|
||||
rem Doing this forcefully is probably not the best method
|
||||
taskkill /PID !_pid! /f>nul
|
||||
)
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_restart
|
||||
echo Unsupported
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_reload
|
||||
echo Unsupported
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_status
|
||||
call :pidexists
|
||||
if %_result%==0 (
|
||||
echo NodeBB is not running
|
||||
echo "nodebb.bat start" to launch the NodeBB server
|
||||
) else (
|
||||
echo NodeBB Running ^(pid !_pid!^)
|
||||
echo "nodebb.bat stop" to stop the NodeBB server
|
||||
echo "nodebb.bat log" to view server output
|
||||
echo "nodebb.bat restart" to restart NodeBB
|
||||
)
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_log
|
||||
cls
|
||||
type .\logs\output.log
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_upgrade
|
||||
call npm install
|
||||
call npm i nodebb-theme-vanilla nodebb-theme-lavender nodebb-widget-essentials
|
||||
node app --upgrade
|
||||
copy /b package.json +,,>nul
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_setup
|
||||
node app --setup %*
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_reset
|
||||
node app --reset --%2
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_dev
|
||||
echo Launching NodeBB in "development" mode.
|
||||
echo To run the production build of NodeBB, please use "forever".
|
||||
echo More Information: https://docs.nodebb.org/en/latest/running/index.html
|
||||
set NODE_ENV=development
|
||||
node loader --no-daemon %*
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:CASE_watch
|
||||
echo Not supported
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:DEFAULT_CASE
|
||||
echo Welcome to NodeBB
|
||||
echo Usage: nodebb.bat ^{start^|stop^|reload^|restart^|log^|setup^|reset^|upgrade^|dev^|watch^}
|
||||
|
||||
goto END_CASE
|
||||
|
||||
:END_CASE
|
||||
endlocal
|
||||
VER > NUL
|
||||
goto :EOF
|
||||
|
||||
:pidexists
|
||||
if exist %~dp0pidfile (
|
||||
set /p _pid=<pidfile
|
||||
|
||||
for /f "usebackq" %%Z in (`tasklist /nh /fi "PID eq !_pid!"`) do (
|
||||
if %%Z==INFO: (
|
||||
del pidfile
|
||||
set _result=0
|
||||
) else (
|
||||
set _result=1
|
||||
)
|
||||
)
|
||||
) else (
|
||||
set _result=0
|
||||
)
|
||||
node ./nodebb %*
|
||||
|
||||
@@ -13,9 +13,8 @@
|
||||
"start": "node loader.js",
|
||||
"lint": "eslint --cache .",
|
||||
"pretest": "npm run lint",
|
||||
"test": "istanbul cover _mocha test",
|
||||
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
|
||||
"test-windows": "_mocha test"
|
||||
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
|
||||
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "~1.5.0",
|
||||
|
||||
@@ -105,7 +105,7 @@
|
||||
} else if (text.slice(i, i + 2) === ']]') {
|
||||
level -= 1;
|
||||
i += 1;
|
||||
} else if (level === 0 && text[i] === ',') {
|
||||
} else if (level === 0 && text[i] === ',' && text[i - 1] !== '\\') {
|
||||
arr.push(text.slice(brk, i).trim());
|
||||
i += 1;
|
||||
brk = i;
|
||||
@@ -260,7 +260,8 @@
|
||||
}
|
||||
var out = translated;
|
||||
translatedArgs.forEach(function (arg, i) {
|
||||
out = out.replace(new RegExp('%' + (i + 1), 'g'), arg);
|
||||
var escaped = arg.replace(/%/g, '%').replace(/\\,/g, ',');
|
||||
out = out.replace(new RegExp('%' + (i + 1), 'g'), escaped);
|
||||
});
|
||||
return out;
|
||||
});
|
||||
@@ -394,9 +395,14 @@
|
||||
|
||||
/**
|
||||
* Construct a translator pattern
|
||||
* @param {string} name - Translation name
|
||||
* @param {...string} arg - Optional argument for the pattern
|
||||
*/
|
||||
Translator.compile = function compile() {
|
||||
var args = Array.prototype.slice.call(arguments, 0);
|
||||
var args = Array.prototype.slice.call(arguments, 0).map(function (text) {
|
||||
// escape commas and percent signs in arguments
|
||||
return text.replace(/%/g, '%').replace(/,/g, ',');
|
||||
});
|
||||
|
||||
return '[[' + args.join(', ') + ']]';
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ var Translator = shim.Translator;
|
||||
|
||||
require('../src/languages').init(function () {});
|
||||
|
||||
describe('translator shim', function () {
|
||||
describe('Translator shim', function () {
|
||||
describe('.translate()', function () {
|
||||
it('should translate correctly', function (done) {
|
||||
shim.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]', function (translated) {
|
||||
@@ -15,6 +15,13 @@ describe('translator shim', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should accept a language parameter and adjust accordingly', function (done) {
|
||||
shim.translate('[[global:home]]', 'de', function (translated) {
|
||||
assert.strictEqual(translated, 'Übersicht');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -27,139 +34,115 @@ describe('new Translator(language)', function () {
|
||||
});
|
||||
|
||||
describe('.translate()', function () {
|
||||
it('should handle basic translations', function (done) {
|
||||
it('should handle basic translations', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[global:home]]').then(function (translated) {
|
||||
return translator.translate('[[global:home]]').then(function (translated) {
|
||||
assert.strictEqual(translated, 'Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys in regular text', function (done) {
|
||||
it('should handle language keys in regular text', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('Let\'s go [[global:home]]').then(function (translated) {
|
||||
return translator.translate('Let\'s go [[global:home]]').then(function (translated) {
|
||||
assert.strictEqual(translated, 'Let\'s go Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should accept a language parameter and adjust accordingly', function (done) {
|
||||
it('should handle language keys in regular text with another language specified', function () {
|
||||
var translator = Translator.create('de');
|
||||
|
||||
translator.translate('[[global:home]]').then(function (translated) {
|
||||
assert.strictEqual(translated, 'Übersicht');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys in regular text with another language specified', function (done) {
|
||||
var translator = Translator.create('de');
|
||||
|
||||
translator.translate('[[global:home]] test').then(function (translated) {
|
||||
return translator.translate('[[global:home]] test').then(function (translated) {
|
||||
assert.strictEqual(translated, 'Übersicht test');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys with parameters', function (done) {
|
||||
it('should handle language keys with parameters', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[global:pagination.out_of, 1, 5]]').then(function (translated) {
|
||||
return translator.translate('[[global:pagination.out_of, 1, 5]]').then(function (translated) {
|
||||
assert.strictEqual(translated, '1 out of 5');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys inside language keys', function (done) {
|
||||
it('should handle language keys inside language keys', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]').then(function (translated) {
|
||||
return translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]').then(function (translated) {
|
||||
assert.strictEqual(translated, 'You are now leaving Guest');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys inside language keys with multiple parameters', function (done) {
|
||||
it('should handle language keys inside language keys with multiple parameters', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]').then(function (translated) {
|
||||
return translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]').then(function (translated) {
|
||||
assert.strictEqual(translated, '<strong>Guest</strong> has posted a reply to: <strong>My Topic</strong>');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle language keys inside language keys with all parameters as language keys', function (done) {
|
||||
it('should handle language keys inside language keys with all parameters as language keys', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[notifications:user_posted_to, [[global:guest]], [[global:guest]]]]').then(function (translated) {
|
||||
return translator.translate('[[notifications:user_posted_to, [[global:guest]], [[global:guest]]]]').then(function (translated) {
|
||||
assert.strictEqual(translated, '<strong>Guest</strong> has posted a reply to: <strong>Guest</strong>');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should properly handle parameters that contain square brackets', function (done) {
|
||||
it('should properly handle parameters that contain square brackets', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]').then(function (translated) {
|
||||
return translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]').then(function (translated) {
|
||||
assert.strictEqual(translated, '[guest] out of Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should properly handle parameters that contain parentheses', function (done) {
|
||||
it('should properly handle parameters that contain parentheses', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]').then(function (translated) {
|
||||
return translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]').then(function (translated) {
|
||||
assert.strictEqual(translated, '(foobar) out of Home');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not translate language key parameters with HTML in them', function (done) {
|
||||
it('should escape language key parameters with HTML in them', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
var key = '[[global:403.login, <strong>test</strong>]]';
|
||||
translator.translate(key).then(function (translated) {
|
||||
return translator.translate(key).then(function (translated) {
|
||||
assert.strictEqual(translated, 'Perhaps you should <a href=\'<strong>test</strong>/login\'>try logging in</a>?');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should properly escape % and ,', function (done) {
|
||||
it('should properly escape and ignore % and \\, in arguments', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
|
||||
var title = 'Test 1, 2, 3 % salmon';
|
||||
title = title.replace(/%/g, '%').replace(/,/g, ',');
|
||||
var title = 'Test 1\\, 2\\, 3 % salmon';
|
||||
var key = "[[topic:composer.replying_to, " + title + "]]";
|
||||
translator.translate(key).then(function (translated) {
|
||||
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
|
||||
done();
|
||||
return translator.translate(key).then(function (translated) {
|
||||
assert.strictEqual(translated, 'Replying to Test 1, 2, 3 % salmon');
|
||||
});
|
||||
});
|
||||
|
||||
it('should not translate [[derp] some text', function (done) {
|
||||
it('should not translate [[derp] some text', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
translator.translate('[[derp] some text').then(function (translated) {
|
||||
return translator.translate('[[derp] some text').then(function (translated) {
|
||||
assert.strictEqual('[[derp] some text', translated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not translate [[derp:xyz] some text', function (done) {
|
||||
it('should not translate [[derp:xyz] some text', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
translator.translate('[[derp:xyz] some text').then(function (translated) {
|
||||
return translator.translate('[[derp:xyz] some text').then(function (translated) {
|
||||
assert.strictEqual('[[derp:xyz] some text', translated);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should translate [[pages:users/latest]] properly', function (done) {
|
||||
it('should translate keys with slashes properly', function () {
|
||||
var translator = Translator.create('en-GB');
|
||||
translator.translate('[[pages:users/latest]]').then(function (translated) {
|
||||
return translator.translate('[[pages:users/latest]]').then(function (translated) {
|
||||
assert.strictEqual(translated, 'Latest Users');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -188,7 +171,7 @@ describe('Translator.create()', function () {
|
||||
});
|
||||
|
||||
describe('Translator modules', function () {
|
||||
it('should work before registered', function (done) {
|
||||
it('should work before registered', function () {
|
||||
var translator = Translator.create();
|
||||
|
||||
Translator.registerModule('test-custom-integer-format', function (lang) {
|
||||
@@ -207,20 +190,16 @@ describe('Translator modules', function () {
|
||||
};
|
||||
});
|
||||
|
||||
translator.translate('[[test-custom-integer-format:octal, 24]]')
|
||||
.then(function (translation) {
|
||||
return translator.translate('[[test-custom-integer-format:octal, 24]]').then(function (translation) {
|
||||
assert.strictEqual(translation, '30');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should work after registered', function (done) {
|
||||
it('should work after registered', function () {
|
||||
var translator = Translator.create('de');
|
||||
|
||||
translator.translate('[[test-custom-integer-format:octal, 23]]')
|
||||
.then(function (translation) {
|
||||
return translator.translate('[[test-custom-integer-format:octal, 23]]').then(function (translation) {
|
||||
assert.strictEqual(translation, '27');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -244,4 +223,41 @@ describe('Translator static methods', function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
describe('.escape', function () {
|
||||
it('should escape translation patterns within text', function (done) {
|
||||
assert.strictEqual(
|
||||
Translator.escape('some nice text [[global:home]] here'),
|
||||
'some nice text \\[\\[global:home\\]\\] here'
|
||||
);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('.unescape', function () {
|
||||
it('should unescape escaped translation patterns within text', function (done) {
|
||||
assert.strictEqual(
|
||||
Translator.unescape('some nice text \\[\\[global:home\\]\\] here'),
|
||||
'some nice text [[global:home]] here'
|
||||
);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('.compile', function () {
|
||||
it('should create a translator pattern from a key and list of arguments', function (done) {
|
||||
assert.strictEqual(
|
||||
Translator.compile('amazing:cool', 'awesome', 'great'),
|
||||
'[[amazing:cool, awesome, great]]'
|
||||
);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should escape `%` and `,` in arguments', function (done) {
|
||||
assert.strictEqual(
|
||||
Translator.compile('amazing:cool', '100% awesome!', 'one, two, and three'),
|
||||
'[[amazing:cool, 100% awesome!, one, two, and three]]'
|
||||
);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user