I18n.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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',
  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, '%%');
  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. });
  52. describe('getPluralForm', function () {
  53. before(function () {
  54. $.PrivateBin.I18n.reset();
  55. });
  56. jsc.property(
  57. 'returns valid key for plural form',
  58. common.jscSupportedLanguages(),
  59. 'integer',
  60. function(language, n) {
  61. $.PrivateBin.I18n.reset(language);
  62. var result = $.PrivateBin.I18n.getPluralForm(n);
  63. // arabic seems to have the highest plural count with 6 forms
  64. return result >= 0 && result <= 5;
  65. }
  66. );
  67. });
  68. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  69. // TODO: This needs to be tested using a browser.
  70. describe('loadTranslations', function () {
  71. this.timeout(30000);
  72. before(function () {
  73. $.PrivateBin.I18n.reset();
  74. });
  75. jsc.property(
  76. 'downloads and handles any supported language',
  77. common.jscSupportedLanguages(),
  78. function(language) {
  79. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  80. $.PrivateBin.I18n.reset('en');
  81. $.PrivateBin.I18n.loadTranslations();
  82. $.PrivateBin.I18n.reset(language, require('../../i18n/' + language + '.json'));
  83. var result = $.PrivateBin.I18n.translate('en'),
  84. alias = $.PrivateBin.I18n._('en');
  85. clean();
  86. return language === result && language === alias;
  87. }
  88. );
  89. jsc.property(
  90. 'should default to en',
  91. function() {
  92. var clean = jsdom('', {url: 'https://privatebin.net/'});
  93. // when navigator.userLanguage is undefined and no default language
  94. // is specified, it would throw an error
  95. [ 'language', 'userLanguage' ].forEach(function (key) {
  96. Object.defineProperty(navigator, key, {
  97. value: undefined,
  98. writeable: false
  99. });
  100. });
  101. $.PrivateBin.I18n.reset('en');
  102. $.PrivateBin.I18n.loadTranslations();
  103. var result = $.PrivateBin.I18n.translate('en'),
  104. alias = $.PrivateBin.I18n._('en');
  105. clean();
  106. return 'en' === result && 'en' === alias;
  107. }
  108. );
  109. });
  110. });