2017-12-14 01:19:05 -05:00
|
|
|
'use strict';
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
describe('I18n', function () {
|
|
|
|
describe('translate', function () {
|
2020-01-18 04:44:35 -05:00
|
|
|
this.timeout(30000);
|
2017-12-14 01:19:05 -05:00
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
2020-01-25 03:07:29 -05:00
|
|
|
jsc.property(
|
|
|
|
'returns message ID unchanged if no translation found',
|
|
|
|
'string',
|
|
|
|
function (messageId) {
|
|
|
|
messageId = messageId.replace(/%(s|d)/g, '%%');
|
|
|
|
var plurals = [messageId, messageId + 's'],
|
|
|
|
fake = [messageId],
|
|
|
|
result = $.PrivateBin.I18n.translate(messageId);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var alias = $.PrivateBin.I18n._(messageId);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var pluralResult = $.PrivateBin.I18n.translate(plurals);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var pluralAlias = $.PrivateBin.I18n._(plurals);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var fakeResult = $.PrivateBin.I18n.translate(fake);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
|
|
|
var fakeAlias = $.PrivateBin.I18n._(fake);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
2020-02-01 01:40:14 -05:00
|
|
|
if (messageId.indexOf('<a') === -1) {
|
|
|
|
messageId = $.PrivateBin.Helper.htmlEntities(messageId);
|
|
|
|
} else {
|
|
|
|
messageId = DOMPurify.sanitize(
|
|
|
|
messageId, {
|
2020-02-01 02:46:59 -05:00
|
|
|
ALLOWED_TAGS: ['a', 'i', 'span'],
|
2020-02-01 01:40:14 -05:00
|
|
|
ALLOWED_ATTR: ['href', 'id']
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2020-01-25 03:07:29 -05:00
|
|
|
return messageId === result && messageId === alias &&
|
|
|
|
messageId === pluralResult && messageId === pluralAlias &&
|
|
|
|
messageId === fakeResult && messageId === fakeAlias;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter, encoding all, when no link is in the messageID',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2020-02-01 01:40:14 -05:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
|
2020-01-25 03:07:29 -05:00
|
|
|
const translation = $.PrivateBin.Helper.htmlEntities(prefix + params[0] + postfix);
|
|
|
|
params.unshift(prefix + '%s' + postfix);
|
|
|
|
const result = $.PrivateBin.I18n.translate.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
const alias = $.PrivateBin.I18n._.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
return translation === result && translation === alias;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter, encoding params only, when a link is part of the messageID',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%');
|
|
|
|
const translation = DOMPurify.sanitize(
|
2020-02-04 12:58:24 -05:00
|
|
|
prefix + '<a href="' + params[0] + '"></a>' + postfix, {
|
2020-02-01 02:46:59 -05:00
|
|
|
ALLOWED_TAGS: ['a', 'i', 'span'],
|
2020-01-25 03:07:29 -05:00
|
|
|
ALLOWED_ATTR: ['href', 'id']
|
|
|
|
}
|
|
|
|
);
|
2020-02-04 12:58:24 -05:00
|
|
|
params.unshift(prefix + '<a href="%s"></a>' + postfix);
|
2020-01-25 03:07:29 -05:00
|
|
|
const result = $.PrivateBin.I18n.translate.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
const alias = $.PrivateBin.I18n._.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
return translation === result && translation === alias;
|
|
|
|
}
|
|
|
|
);
|
2020-01-18 04:44:35 -05:00
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter into an element, encoding all, when no link is in the messageID',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2020-02-01 01:40:14 -05:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%');
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%').replace(/<a/g, '');
|
2020-01-31 16:42:42 -05:00
|
|
|
const translation = $('<textarea>').text((prefix + params[0] + postfix)).text();
|
|
|
|
let args = Array.prototype.slice.call(params);
|
|
|
|
args.unshift(prefix + '%s' + postfix);
|
2020-01-18 04:44:35 -05:00
|
|
|
let clean = jsdom();
|
|
|
|
$('body').html('<div id="i18n"></div>');
|
2020-01-31 16:42:42 -05:00
|
|
|
args.unshift($('#i18n'));
|
|
|
|
$.PrivateBin.I18n.translate.apply(this, args);
|
2020-01-18 04:44:35 -05:00
|
|
|
const result = $('#i18n').text();
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
clean();
|
|
|
|
clean = jsdom();
|
|
|
|
$('body').html('<div id="i18n"></div>');
|
2020-01-31 16:42:42 -05:00
|
|
|
args[0] = $('#i18n');
|
|
|
|
$.PrivateBin.I18n._.apply(this, args);
|
2020-01-18 04:44:35 -05:00
|
|
|
const alias = $('#i18n').text();
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
clean();
|
|
|
|
return translation === result && translation === alias;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter into an element, encoding params only, when a link is part of the messageID inserted',
|
|
|
|
'string',
|
|
|
|
'(small nearray) string',
|
|
|
|
'string',
|
|
|
|
function (prefix, params, postfix) {
|
2020-01-31 16:42:42 -05:00
|
|
|
prefix = prefix.replace(/%(s|d)/g, '%%').trim();
|
|
|
|
params[0] = params[0].replace(/%(s|d)/g, '%%').trim();
|
|
|
|
postfix = postfix.replace(/%(s|d)/g, '%%').trim();
|
|
|
|
const translation = DOMPurify.sanitize(
|
2020-02-04 12:58:24 -05:00
|
|
|
prefix + '<a href="' + params[0] + '"></a>' + postfix, {
|
2020-02-01 02:46:59 -05:00
|
|
|
ALLOWED_TAGS: ['a', 'i', 'span'],
|
2020-01-18 04:44:35 -05:00
|
|
|
ALLOWED_ATTR: ['href', 'id']
|
|
|
|
}
|
2020-01-31 16:42:42 -05:00
|
|
|
);
|
2020-01-18 04:44:35 -05:00
|
|
|
let args = Array.prototype.slice.call(params);
|
2020-02-04 12:58:24 -05:00
|
|
|
args.unshift(prefix + '<a href="%s"></a>' + postfix);
|
2020-01-18 04:44:35 -05:00
|
|
|
let clean = jsdom();
|
|
|
|
$('body').html('<div id="i18n"></div>');
|
|
|
|
args.unshift($('#i18n'));
|
|
|
|
$.PrivateBin.I18n.translate.apply(this, args);
|
|
|
|
const result = $('#i18n').html();
|
2020-01-18 01:44:32 -05:00
|
|
|
$.PrivateBin.I18n.reset();
|
2020-01-18 04:44:35 -05:00
|
|
|
clean();
|
|
|
|
clean = jsdom();
|
|
|
|
$('body').html('<div id="i18n"></div>');
|
|
|
|
args[0] = $('#i18n');
|
|
|
|
$.PrivateBin.I18n._.apply(this, args);
|
|
|
|
const alias = $('#i18n').html();
|
2020-01-18 01:44:32 -05:00
|
|
|
$.PrivateBin.I18n.reset();
|
2020-01-18 04:44:35 -05:00
|
|
|
clean();
|
2020-01-18 01:44:32 -05:00
|
|
|
return translation === result && translation === alias;
|
|
|
|
}
|
|
|
|
);
|
2017-12-14 01:19:05 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getPluralForm', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'returns valid key for plural form',
|
|
|
|
common.jscSupportedLanguages(),
|
|
|
|
'integer',
|
|
|
|
function(language, n) {
|
|
|
|
$.PrivateBin.I18n.reset(language);
|
|
|
|
var result = $.PrivateBin.I18n.getPluralForm(n);
|
|
|
|
// arabic seems to have the highest plural count with 6 forms
|
|
|
|
return result >= 0 && result <= 5;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// loading of JSON via AJAX needs to be tested in the browser, this just mocks it
|
|
|
|
// TODO: This needs to be tested using a browser.
|
|
|
|
describe('loadTranslations', function () {
|
|
|
|
this.timeout(30000);
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'downloads and handles any supported language',
|
|
|
|
common.jscSupportedLanguages(),
|
|
|
|
function(language) {
|
2018-12-25 10:42:18 -05:00
|
|
|
// cleanup
|
|
|
|
var clean = jsdom('', {cookie: ['lang=en']});
|
2017-12-14 01:19:05 -05:00
|
|
|
$.PrivateBin.I18n.reset('en');
|
|
|
|
$.PrivateBin.I18n.loadTranslations();
|
2018-12-25 10:42:18 -05:00
|
|
|
clean();
|
|
|
|
|
|
|
|
// mock
|
|
|
|
clean = jsdom('', {cookie: ['lang=' + language]});
|
2017-12-14 01:19:05 -05:00
|
|
|
$.PrivateBin.I18n.reset(language, require('../../i18n/' + language + '.json'));
|
2023-09-19 01:29:00 -04:00
|
|
|
var loadedLang = $.PrivateBin.I18n.getLanguage(),
|
|
|
|
result = $.PrivateBin.I18n.translate('Never'),
|
|
|
|
alias = $.PrivateBin.I18n._('Never');
|
2017-12-14 01:19:05 -05:00
|
|
|
clean();
|
2023-09-19 01:29:00 -04:00
|
|
|
return language === loadedLang && result === alias;
|
2017-12-14 01:19:05 -05:00
|
|
|
}
|
|
|
|
);
|
2018-04-06 23:22:26 -04:00
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'should default to en',
|
|
|
|
function() {
|
|
|
|
var clean = jsdom('', {url: 'https://privatebin.net/'});
|
|
|
|
|
2018-04-09 08:13:18 -04:00
|
|
|
// when navigator.userLanguage is undefined and no default language
|
|
|
|
// is specified, it would throw an error
|
2018-04-06 23:22:26 -04:00
|
|
|
[ 'language', 'userLanguage' ].forEach(function (key) {
|
|
|
|
Object.defineProperty(navigator, key, {
|
|
|
|
value: undefined,
|
|
|
|
writeable: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$.PrivateBin.I18n.reset('en');
|
|
|
|
$.PrivateBin.I18n.loadTranslations();
|
2023-09-19 01:29:00 -04:00
|
|
|
var result = $.PrivateBin.I18n.translate('Never'),
|
|
|
|
alias = $.PrivateBin.I18n._('Never');
|
2018-04-06 23:22:26 -04:00
|
|
|
|
|
|
|
clean();
|
2023-09-19 01:29:00 -04:00
|
|
|
return 'Never' === result && 'Never' === alias;
|
2018-04-06 23:22:26 -04:00
|
|
|
}
|
|
|
|
);
|
2017-12-14 01:19:05 -05:00
|
|
|
});
|
|
|
|
});
|