I18n.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. return messageId === result && messageId === alias &&
  28. messageId === pluralResult && messageId === pluralAlias &&
  29. messageId === fakeResult && messageId === fakeAlias;
  30. }
  31. );
  32. jsc.property(
  33. 'replaces %s in strings with first given parameter',
  34. 'string',
  35. '(small nearray) string',
  36. 'string',
  37. function (prefix, params, postfix) {
  38. prefix = prefix.replace(/%(s|d)/g, '%%');
  39. params[0] = params[0].replace(/%(s|d)/g, '%%');
  40. postfix = postfix.replace(/%(s|d)/g, '%%');
  41. var translation = prefix + params[0] + postfix;
  42. params.unshift(prefix + '%s' + postfix);
  43. var result = $.PrivateBin.I18n.translate.apply(this, params);
  44. $.PrivateBin.I18n.reset();
  45. var alias = $.PrivateBin.I18n._.apply(this, params);
  46. $.PrivateBin.I18n.reset();
  47. return translation === result && translation === alias;
  48. }
  49. );
  50. });
  51. describe('getPluralForm', function () {
  52. before(function () {
  53. $.PrivateBin.I18n.reset();
  54. });
  55. jsc.property(
  56. 'returns valid key for plural form',
  57. common.jscSupportedLanguages(),
  58. 'integer',
  59. function(language, n) {
  60. $.PrivateBin.I18n.reset(language);
  61. var result = $.PrivateBin.I18n.getPluralForm(n);
  62. // arabic seems to have the highest plural count with 6 forms
  63. return result >= 0 && result <= 5;
  64. }
  65. );
  66. });
  67. // loading of JSON via AJAX needs to be tested in the browser, this just mocks it
  68. // TODO: This needs to be tested using a browser.
  69. describe('loadTranslations', function () {
  70. this.timeout(30000);
  71. before(function () {
  72. $.PrivateBin.I18n.reset();
  73. });
  74. jsc.property(
  75. 'downloads and handles any supported language',
  76. common.jscSupportedLanguages(),
  77. function(language) {
  78. var clean = jsdom('', {url: 'https://privatebin.net/', cookie: ['lang=' + language]});
  79. $.PrivateBin.I18n.reset('en');
  80. $.PrivateBin.I18n.loadTranslations();
  81. $.PrivateBin.I18n.reset(language, require('../../i18n/' + language + '.json'));
  82. var result = $.PrivateBin.I18n.translate('en'),
  83. alias = $.PrivateBin.I18n._('en');
  84. clean();
  85. return language === result && language === alias;
  86. }
  87. );
  88. jsc.property(
  89. 'should default to en',
  90. function() {
  91. var clean = jsdom('', {url: 'https://privatebin.net/'});
  92. // when navigator.userLanguage is undefined and no default language
  93. // is specified, it would throw an error
  94. [ 'language', 'userLanguage' ].forEach(function (key) {
  95. Object.defineProperty(navigator, key, {
  96. value: undefined,
  97. writeable: false
  98. });
  99. });
  100. $.PrivateBin.I18n.reset('en');
  101. $.PrivateBin.I18n.loadTranslations();
  102. var result = $.PrivateBin.I18n.translate('en'),
  103. alias = $.PrivateBin.I18n._('en');
  104. clean();
  105. return 'en' === result && 'en' === alias;
  106. }
  107. );
  108. });
  109. });