You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/test/translator.js

381 lines
13 KiB
JavaScript

'use strict';
// For tests relating to Transifex configuration, check i18n.js
const assert = require('assert');
Webpack5 (#10311) * feat: webpack 5 part 1 * fix: gruntfile fixes * fix: fix taskbar warning add app.importScript copy public/src/modules to build folder * refactor: remove commented old code * feat: reenable admin * fix: acp settings pages, fix sortable on manage categories embedded require in html not allowed * fix: bundle serialize/deserizeli so plugins dont break * test: fixe util tests * test: fix require path * test: more test fixes * test: require correct utils module * test: require correct utils * test: log stack * test: fix db require blowing up tests * test: move and disable bundle test * refactor: add aliases * test: disable testing route * fix: move webpack modules necessary for build, into `dependencies` * test: fix one more test remove 500-embed.tpl * fix: restore use of assets/nodebb.min.js, at least for now * fix: remove unnecessary line break * fix: point to proper ACP bundle * test: maybe fix build test * test: composer * refactor: dont need dist * refactor: more cleanup use everything from build/public folder * get rid of conditional import in app.js * fix: ace * refactor: cropper alias * test: lint and test fixes * lint: fix * refactor: rename function to app.require * refactor: go back to using app.require * chore: use github branch * chore: use webpack branch * feat: webpack webinstaller * feat: add chunkFile name with contenthash * refactor: move hooks to top * refactor: get rid of template500Function * fix(deps): use webpack5 branch of 2factor plugin * chore: tagging v2.0.0-beta.0 pre-release version :boom: :shipit: :tada: :rocket: * refactor: disable cache on templates loadTemplate is called once by benchpress and the result is cache internally * refactor: add server side helpers.js * feat: deprecate /plugins shorthand route, closes #10343 * refactor: use build/public for webpack * test: fix filename * fix: more specific selector * lint: ignore * refactor: fix comments * test: add debug for random failing test * refactor: cleanup remove test page, remove dupe functions in utils.common * lint: use relative path for now * chore: bump prerelease version * feat: add translateKeys * fix: optional params * fix: get rid of extra timeago files * refactor: cleanup, require timeago locale earlier remove translator.prepareDOM, it is in header.tpl html tag * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels (#10378) * refactor: privileges system to use a Map in the backend instead of separate objects for keys and labels - Existing hooks are preserved (to be deprecated at a later date, possibly) - New init hooks are called on NodeBB start, and provide a one-stop shop to add new privileges, instead of having to add to four different hooks * docs: fix typo in comment * test: spec changes * feat: allow app.require('bootbox'/'benchpressjs') * refactor: require server side utils * test: jquery ready * change istaller to use build/public * test: use document.addEventListener * refactor: closes #10301 * refactor: generateTopicClass * fix: column counts for other privileges * fix: #10443, regression where sorted-list items did not render into the DOM in the predicted order [breaking] * fix: typo in hook name * refactor: introduce a generic autocomplete.init() method that can be called to add nodebb-style autocompletion but using different data sources (e.g. not user/groups/tags) * fix: crash if `delay` not passed in (as it cannot be destructured) * refactor: replace substr * feat: set --panel-offset style in html element based on stored value in localStorage * refactor: addDropupHandler() logic to be less naive - Take into account height of the menu - Don't apply dropUp logic if there's nothing in the dropdown - Remove 'hidden' class (added by default in Persona for post tools) when menu items are added closes #10423 * refactor: simplify utils.params [breaking] Retrospective analysis of the usage of this method suggests that the options passed in are superfluous, and that only `url` is required. Using a browser built-in makes more sense to accomplish what this method sets out to do. * feat: add support for returning full URLSearchParams for utils.params * fix: utils.params() fallback handling * fix: default empty obj for params() * fix: remove \'loggedin\' and \'register\' qs parameters once they have been used, delay invocation of messages until ajaxify.end * fix: utils.params() not allowing relative paths to be passed in * refactor(DRY): new assertPasswordValidity utils method * fix: incorrect error message returned on insufficient privilege on flag edit * fix: read/update/delete access to flags API should be limited for moderators to only post flags in categories they moderate - added failing tests and patched up middleware.assert.flags to fix * refactor: flag api v3 tests to create new post and flags on every round * fix: missing error:no-flag language key * refactor: flags.canView to check flag existence, simplify middleware.assert.flag * feat: flag deletion API endpoint, #10426 * feat: UI for flag deletion, closes #10426 * chore: update plugin versions * chore: up emoji * chore: update markdown * chore: up emoji-android * fix: regression caused by utils.params() refactor, supports arrays and pipes all values through utils.toType, adjusts tests to type check Co-authored-by: Julian Lam <[email protected]>
3 years ago
const shim = require('../src/translator');
const { Translator } = shim;
const db = require('./mocks/databasemock');
describe('Translator shim', () => {
describe('.translate()', () => {
it('should translate correctly', (done) => {
shim.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]', (translated) => {
assert.strictEqual(translated, '(foobar) out of Home');
done();
});
});
it('should accept a language parameter and adjust accordingly', (done) => {
shim.translate('[[global:home]]', 'de', (translated) => {
assert.strictEqual(translated, 'Übersicht');
done();
});
});
it('should translate empty string properly', (done) => {
shim.translate('', 'en-GB', (translated) => {
assert.strictEqual(translated, '');
done();
});
});
it('should translate empty string properly', async () => {
const translated = await shim.translate('', 'en-GB');
assert.strictEqual(translated, '');
});
it('should not allow path traversal', async () => {
const t = await shim.translate('[[../../../../config:secret]]');
assert.strictEqual(t, 'secret');
});
});
});
describe('new Translator(language)', () => {
it('should throw if not passed a language', (done) => {
assert.throws(() => {
new Translator();
}, /language string/);
done();
});
describe('.translate()', () => {
it('should handle basic translations', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[global:home]]').then((translated) => {
assert.strictEqual(translated, 'Home');
});
});
it('should handle language keys in regular text', () => {
const translator = Translator.create('en-GB');
return translator.translate('Let\'s go [[global:home]]').then((translated) => {
assert.strictEqual(translated, 'Let\'s go Home');
});
});
it('should handle language keys in regular text with another language specified', () => {
const translator = Translator.create('de');
return translator.translate('[[global:home]] test').then((translated) => {
assert.strictEqual(translated, 'Übersicht test');
});
});
it('should handle language keys with parameters', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[global:pagination.out_of, 1, 5]]').then((translated) => {
assert.strictEqual(translated, '1 out of 5');
});
});
it('should handle language keys inside language keys', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[notifications:outgoing_link_message, [[global:guest]]]]').then((translated) => {
assert.strictEqual(translated, 'You are now leaving Guest');
});
});
it('should handle language keys inside language keys with multiple parameters', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[notifications:user_posted_to, [[global:guest]], My Topic]]').then((translated) => {
assert.strictEqual(translated, '<strong>Guest</strong> has posted a reply to: <strong>My Topic</strong>');
});
});
it('should handle language keys inside language keys with all parameters as language keys', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[notifications:user_posted_to, [[global:guest]], [[global:guest]]]]').then((translated) => {
assert.strictEqual(translated, '<strong>Guest</strong> has posted a reply to: <strong>Guest</strong>');
});
});
it('should properly handle parameters that contain square brackets', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[global:pagination.out_of, [guest], [[global:home]]]]').then((translated) => {
assert.strictEqual(translated, '[guest] out of Home');
});
});
it('should properly handle parameters that contain parentheses', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[global:pagination.out_of, (foobar), [[global:home]]]]').then((translated) => {
assert.strictEqual(translated, '(foobar) out of Home');
});
});
it('should escape language key parameters with HTML in them', () => {
const translator = Translator.create('en-GB');
const key = '[[global:403.login, <strong>test</strong>]]';
return translator.translate(key).then((translated) => {
assert.strictEqual(translated, 'Perhaps you should <a href=\'&lt;strong&gt;test&lt;/strong&gt;/login\'>try logging in</a>?');
});
});
it('should not unescape html in parameters', () => {
const translator = Translator.create('en-GB');
7 years ago
const key = '[[pages:tag, some&amp;tag]]';
return translator.translate(key).then((translated) => {
7 years ago
assert.strictEqual(translated, 'Topics tagged under &quot;some&amp;tag&quot;');
});
});
it('should translate escaped translation arguments properly', () => {
// https://github.com/NodeBB/NodeBB/issues/9206
const translator = Translator.create('en-GB');
const key = '[[notifications:upvoted_your_post_in, test1, error: Error: &lsqb;&lsqb;error:group-name-too-long&rsqb;&rsqb; on NodeBB Upgrade]]';
return translator.translate(key).then((translated) => {
assert.strictEqual(translated, '<strong>test1</strong> has upvoted your post in <strong>error: Error: &lsqb;&lsqb;error:group-name-too-long&rsqb;&rsqb; on NodeBB Upgrade</strong>.');
});
});
it('should properly escape and ignore % and \\, in arguments', () => {
const translator = Translator.create('en-GB');
const title = 'Test 1\\, 2\\, 3 %2 salmon';
const key = `[[topic:composer.replying_to, ${title}]]`;
return translator.translate(key).then((translated) => {
8 years ago
assert.strictEqual(translated, 'Replying to Test 1&#44; 2&#44; 3 &#37;2 salmon');
});
});
it('should not escape regular %', () => {
const translator = Translator.create('en-GB');
8 years ago
const title = '3 % salmon';
const key = `[[topic:composer.replying_to, ${title}]]`;
return translator.translate(key).then((translated) => {
8 years ago
assert.strictEqual(translated, 'Replying to 3 % salmon');
9 years ago
});
});
it('should not translate [[derp] some text', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[derp] some text').then((translated) => {
assert.strictEqual('[[derp] some text', translated);
});
});
it('should not translate [[derp]] some text', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[derp]] some text').then((translated) => {
6 years ago
assert.strictEqual('[[derp]] some text', translated);
});
});
it('should not translate [[derp:xyz] some text', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[derp:xyz] some text').then((translated) => {
assert.strictEqual('[[derp:xyz] some text', translated);
});
});
it('should translate keys with slashes properly', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[pages:users/latest]]').then((translated) => {
assert.strictEqual(translated, 'Latest Users');
});
});
it('should use key for unknown keys without arguments', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[unknown:key.without.args]]').then((translated) => {
assert.strictEqual(translated, 'key.without.args');
});
});
it('should use backup for unknown keys with arguments', () => {
const translator = Translator.create('en-GB');
return translator.translate('[[unknown:key.with.args, arguments are here, derpity, derp]]').then((translated) => {
assert.strictEqual(translated, 'unknown:key.with.args, arguments are here, derpity, derp');
});
});
it('should ignore unclosed tokens', () => {
const translator = Translator.create('en-GB');
return translator.translate('here is some stuff and other things [[abc:xyz, other random stuff should be fine here [[global:home]] and more things [[pages:users/latest]]').then((translated) => {
assert.strictEqual(translated, 'here is some stuff and other things abc:xyz, other random stuff should be fine here Home and more things Latest Users');
});
});
});
});
describe('Translator.create()', () => {
it('should return an instance of Translator', (done) => {
const translator = Translator.create('en-GB');
assert(translator instanceof Translator);
done();
});
it('should return the same object for the same language', (done) => {
const one = Translator.create('de');
const two = Translator.create('de');
assert.strictEqual(one, two);
done();
});
it('should default to defaultLang', (done) => {
const translator = Translator.create();
assert.strictEqual(translator.lang, 'en-GB');
done();
});
});
describe('Translator modules', () => {
it('should work before registered', () => {
const translator = Translator.create();
8 years ago
Translator.registerModule('test-custom-integer-format', lang => function (key, args) {
const num = parseInt(args[0], 10) || 0;
if (key === 'binary') {
return num.toString(2);
}
if (key === 'hex') {
return num.toString(16);
}
if (key === 'octal') {
return num.toString(8);
}
return num.toString();
});
return translator.translate('[[test-custom-integer-format:octal, 24]]').then((translation) => {
assert.strictEqual(translation, '30');
});
});
it('should work after registered', () => {
const translator = Translator.create('de');
return translator.translate('[[test-custom-integer-format:octal, 23]]').then((translation) => {
assert.strictEqual(translation, '27');
});
});
it('registerModule be passed the language', (done) => {
Translator.registerModule('something', (lang) => {
assert.ok(lang);
});
const translator = Translator.create('fr_FR');
done();
});
});
describe('Translator static methods', () => {
describe('.removePatterns', () => {
it('should remove translator patterns from text', (done) => {
assert.strictEqual(
Translator.removePatterns('Lorem ipsum dolor [[sit:amet]], consectetur adipiscing elit. [[sed:vitae, [[semper:dolor]]]] lorem'),
'Lorem ipsum dolor , consectetur adipiscing elit. lorem'
);
done();
});
});
describe('.escape', () => {
it('should escape translation patterns within text', (done) => {
assert.strictEqual(
Translator.escape('some nice text [[global:home]] here'),
'some nice text &lsqb;&lsqb;global:home&rsqb;&rsqb; here'
);
done();
});
});
describe('.unescape', () => {
it('should unescape escaped translation patterns within text', (done) => {
assert.strictEqual(
Translator.unescape('some nice text \\[\\[global:home\\]\\] here'),
'some nice text [[global:home]] here'
);
assert.strictEqual(
Translator.unescape('some nice text &lsqb;&lsqb;global:home&rsqb;&rsqb; here'),
'some nice text [[global:home]] here'
);
done();
});
});
describe('.compile', () => {
it('should create a translator pattern from a key and list of arguments', (done) => {
assert.strictEqual(
Translator.compile('amazing:cool', 'awesome', 'great'),
'[[amazing:cool, awesome, great]]'
);
done();
});
it('should escape `%` and `,` in arguments', (done) => {
assert.strictEqual(
Translator.compile('amazing:cool', '100% awesome!', 'one, two, and three'),
'[[amazing:cool, 100&#37; awesome!, one&#44; two&#44; and three]]'
);
done();
});
});
describe('add translation', () => {
it('should add custom translations', async () => {
shim.addTranslation('en-GB', 'my-namespace', { foo: 'a custom translation' });
const t = await shim.translate('this is best [[my-namespace:foo]]');
assert.strictEqual(t, 'this is best a custom translation');
});
});
describe('translate nested keys', () => {
it('should handle nested translations', async () => {
shim.addTranslation('en-GB', 'my-namespace', {
key: {
key1: 'key1 translated',
key2: {
key3: 'key3 translated',
},
},
});
const t1 = await shim.translate('this is best [[my-namespace:key.key1]]');
const t2 = await shim.translate('this is best [[my-namespace:key.key2.key3]]');
assert.strictEqual(t1, 'this is best key1 translated');
assert.strictEqual(t2, 'this is best key3 translated');
});
it("should try the defaults if it didn't reach a string in a nested translation", async () => {
shim.addTranslation('en-GB', 'my-namespace', {
default1: {
default1: 'default1 translated',
'': 'incorrect priority',
},
default2: {
'': 'default2 translated',
},
});
const d1 = await shim.translate('this is best [[my-namespace:default1]]');
const d2 = await shim.translate('this is best [[my-namespace:default2]]');
assert.strictEqual(d1, 'this is best default1 translated');
assert.strictEqual(d2, 'this is best default2 translated');
});
});
});