I18n.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. var common = require('../common');
  3. describe('I18n', function () {
  4. describe('translate', function () {
  5. before(function () {
  6. $.PrivateBin.I18n.reset();
  7. });
  8. jsc.property(
  9. 'returns message ID unchanged if no translation found',
  10. 'string',
  11. function (messageId) {
  12. messageId = messageId.replace(/%(s|d)/g, '%%');
  13. var plurals = [messageId, messageId + 's'],
  14. fake = [messageId],
  15. result = $.PrivateBin.I18n.translate(messageId);
  16. $.PrivateBin.I18n.reset();
  17. var alias = $.PrivateBin.I18n._(messageId);
  18. $.PrivateBin.I18n.reset();
  19. var pluralResult = $.PrivateBin.I18n.translate(plurals);
  20. $.PrivateBin.I18n.reset();
  21. var pluralAlias = $.PrivateBin.I18n._(plurals);
  22. $.PrivateBin.I18n.reset();
  23. var fakeResult = $.PrivateBin.I18n.translate(fake);
  24. $.PrivateBin.I18n.reset();
  25. var fakeAlias = $.PrivateBin.I18n._(fake);
  26. $.PrivateBin.I18n.reset();
  27. messageId = $.PrivateBin.Helper.htmlEntities(messageId);
  28. return messageId === result && messageId === alias &&
  29. messageId === pluralResult && messageId === pluralAlias &&
  30. messageId === fakeResult && messageId === fakeAlias;
  31. }
  32. );
  33. jsc.property(
  34. 'replaces %s in strings with first given parameter, encoding all, when no link is in the messageID',
  35. 'string',
  36. '(small nearray) string',
  37. 'string',
  38. function (prefix, params, postfix) {
  39. prefix = prefix.replace(/%(s|d)/g, '%%');
  40. params[0] = params[0].replace(/%(s|d)/g, '%%').replace(/%<a/g, '');
  41. postfix = postfix.replace(/%(s|d)/g, '%%');
  42. var translation = $.PrivateBin.Helper.htmlEntities(prefix + params[0] + postfix);
  43. params.unshift(prefix + '%s' + postfix);
  44. var result = $.PrivateBin.I18n.translate.apply(this, params);
  45. $.PrivateBin.I18n.reset();
  46. var alias = $.PrivateBin.I18n._.apply(this, params);
  47. $.PrivateBin.I18n.reset();
  48. return translation === result && translation === alias;
  49. }
  50. );
  51. jsc.property(
  52. 'replaces %s in strings with first given parameter, encoding params only, when a link is part of the messageID',
  53. 'string',
  54. '(small nearray) string',
  55. 'string',
  56. function (prefix, params, postfix) {
  57. prefix = prefix.replace(/%(s|d)/g, '%%');
  58. params[0] = params[0].replace(/%(s|d)/g, '%%') + '<a/>';
  59. postfix = postfix.replace(/%(s|d)/g, '%%');
  60. var translation = $.PrivateBin.Helper.htmlEntities(prefix + params[0] + postfix);
  61. params.unshift(prefix + '%s' + postfix);
  62. var result = $.PrivateBin.I18n.translate.apply(this, params);
  63. $.PrivateBin.I18n.reset();
  64. var alias = $.PrivateBin.I18n._.apply(this, params);
  65. $.PrivateBin.I18n.reset();
  66. return translation === result && translation === alias;
  67. }
  68. );
  69. });
  70. describe('getPluralForm', function () {
  71. before(function () {
  72. $.PrivateBin.I18n.reset();
  73. });
  74. jsc.property(
  75. 'returns valid key for plural form',
  76. common.jscSupportedLanguages(),
  77. 'integer',
  78. function(language, n) {
  79. $.PrivateBin.I18n.reset(language);
  80. var result = $.PrivateBin.I18n.getPluralForm(n);
  81. // arabic seems to have the highest plural count with 6 forms
  82. return result >= 0 && result <= 5;
  83. }
  84. );
  85. });
  86. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  87. // TODO: This needs to be tested using a browser.
  88. describe('loadTranslations', function () {
  89. this.timeout(30000);
  90. before(function () {
  91. $.PrivateBin.I18n.reset();
  92. });
  93. jsc.property(
  94. 'downloads and handles any supported language',
  95. common.jscSupportedLanguages(),
  96. function(language) {
  97. // cleanup
  98. var clean = jsdom('', {cookie: ['lang=en']});
  99. $.PrivateBin.I18n.reset('en');
  100. $.PrivateBin.I18n.loadTranslations();
  101. clean();
  102. // mock
  103. clean = jsdom('', {cookie: ['lang=' + language]});
  104. $.PrivateBin.I18n.reset(language, require('../../i18n/' + language + '.json'));
  105. var result = $.PrivateBin.I18n.translate('en'),
  106. alias = $.PrivateBin.I18n._('en');
  107. clean();
  108. return language === result && language === alias;
  109. }
  110. );
  111. jsc.property(
  112. 'should default to en',
  113. function() {
  114. var clean = jsdom('', {url: 'https://privatebin.net/'});
  115. // when navigator.userLanguage is undefined and no default language
  116. // is specified, it would throw an error
  117. [ 'language', 'userLanguage' ].forEach(function (key) {
  118. Object.defineProperty(navigator, key, {
  119. value: undefined,
  120. writeable: false
  121. });
  122. });
  123. $.PrivateBin.I18n.reset('en');
  124. $.PrivateBin.I18n.loadTranslations();
  125. var result = $.PrivateBin.I18n.translate('en'),
  126. alias = $.PrivateBin.I18n._('en');
  127. clean();
  128. return 'en' === result && 'en' === alias;
  129. }
  130. );
  131. });
  132. });