2017-12-14 14:19:05 +08:00
|
|
|
'use strict';
|
|
|
|
var common = require('../common');
|
|
|
|
|
|
|
|
describe('I18n', function () {
|
|
|
|
describe('translate', function () {
|
|
|
|
before(function () {
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2018-01-06 16:26:10 +08:00
|
|
|
var pluralResult = $.PrivateBin.I18n.translate(plurals);
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
2018-01-06 16:26:10 +08:00
|
|
|
var pluralAlias = $.PrivateBin.I18n._(plurals);
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
2018-01-06 16:26:10 +08:00
|
|
|
var fakeResult = $.PrivateBin.I18n.translate(fake);
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
2018-01-06 16:26:10 +08:00
|
|
|
var fakeAlias = $.PrivateBin.I18n._(fake);
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
|
2020-01-04 18:34:16 +08:00
|
|
|
messageId = $.PrivateBin.Helper.htmlEntities(messageId);
|
2017-12-14 14:19:05 +08:00
|
|
|
return messageId === result && messageId === alias &&
|
2018-01-06 16:26:10 +08:00
|
|
|
messageId === pluralResult && messageId === pluralAlias &&
|
|
|
|
messageId === fakeResult && messageId === fakeAlias;
|
2017-12-14 14:19:05 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
jsc.property(
|
|
|
|
'replaces %s in strings with first given parameter',
|
|
|
|
'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, '%%');
|
2020-01-04 18:34:16 +08:00
|
|
|
var translation = $.PrivateBin.Helper.htmlEntities(prefix + params[0] + postfix);
|
2017-12-14 14:19:05 +08:00
|
|
|
params.unshift(prefix + '%s' + postfix);
|
|
|
|
var result = $.PrivateBin.I18n.translate.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
var alias = $.PrivateBin.I18n._.apply(this, params);
|
|
|
|
$.PrivateBin.I18n.reset();
|
|
|
|
return translation === result && translation === alias;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 23:42:18 +08:00
|
|
|
// cleanup
|
|
|
|
var clean = jsdom('', {cookie: ['lang=en']});
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset('en');
|
|
|
|
$.PrivateBin.I18n.loadTranslations();
|
2018-12-25 23:42:18 +08:00
|
|
|
clean();
|
|
|
|
|
|
|
|
// mock
|
|
|
|
clean = jsdom('', {cookie: ['lang=' + language]});
|
2017-12-14 14:19:05 +08:00
|
|
|
$.PrivateBin.I18n.reset(language, require('../../i18n/' + language + '.json'));
|
|
|
|
var result = $.PrivateBin.I18n.translate('en'),
|
|
|
|
alias = $.PrivateBin.I18n._('en');
|
|
|
|
clean();
|
|
|
|
return language === result && language === alias;
|
|
|
|
}
|
|
|
|
);
|
2018-04-07 11:22:26 +08:00
|
|
|
|
|
|
|
jsc.property(
|
|
|
|
'should default to en',
|
|
|
|
function() {
|
|
|
|
var clean = jsdom('', {url: 'https://privatebin.net/'});
|
|
|
|
|
2018-04-09 20:13:18 +08:00
|
|
|
// when navigator.userLanguage is undefined and no default language
|
|
|
|
// is specified, it would throw an error
|
2018-04-07 11:22:26 +08:00
|
|
|
[ 'language', 'userLanguage' ].forEach(function (key) {
|
|
|
|
Object.defineProperty(navigator, key, {
|
|
|
|
value: undefined,
|
|
|
|
writeable: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$.PrivateBin.I18n.reset('en');
|
|
|
|
$.PrivateBin.I18n.loadTranslations();
|
|
|
|
var result = $.PrivateBin.I18n.translate('en'),
|
|
|
|
alias = $.PrivateBin.I18n._('en');
|
|
|
|
|
|
|
|
clean();
|
|
|
|
return 'en' === result && 'en' === alias;
|
|
|
|
}
|
|
|
|
);
|
2017-12-14 14:19:05 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|