l10nSpec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. describe('OC.L10N tests', function() {
  11. var TEST_APP = 'jsunittestapp';
  12. beforeEach(function() {
  13. window._oc_appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
  14. });
  15. afterEach(function() {
  16. OC.L10N._unregister(TEST_APP);
  17. delete window._oc_appswebroots[TEST_APP];
  18. });
  19. describe('text translation', function() {
  20. beforeEach(function() {
  21. spyOn(console, 'warn');
  22. OC.L10N.register(TEST_APP, {
  23. 'Hello world!': 'Hallo Welt!',
  24. 'Hello {name}, the weather is {weather}': 'Hallo {name}, das Wetter ist {weather}',
  25. 'sunny': 'sonnig'
  26. });
  27. });
  28. it('returns untranslated text when no bundle exists', function() {
  29. OC.L10N._unregister(TEST_APP);
  30. expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
  31. });
  32. it('returns untranslated text when no key exists', function() {
  33. expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
  34. });
  35. it('returns translated text when key exists', function() {
  36. expect(t(TEST_APP, 'Hello world!')).toEqual('Hallo Welt!');
  37. });
  38. it('returns translated text with placeholder', function() {
  39. expect(
  40. t(TEST_APP, 'Hello {name}, the weather is {weather}', {name: 'Steve', weather: t(TEST_APP, 'sunny')})
  41. ).toEqual('Hallo Steve, das Wetter ist sonnig');
  42. });
  43. it('returns text with escaped placeholder', function() {
  44. expect(
  45. t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'})
  46. ).toEqual('Hello &lt;strong&gt;Steve&lt;/strong&gt;');
  47. });
  48. it('returns text with not escaped placeholder', function() {
  49. expect(
  50. t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'}, null, {escape: false})
  51. ).toEqual('Hello <strong>Steve</strong>');
  52. });
  53. it('uses DOMPurify to escape the text', function() {
  54. expect(
  55. t(TEST_APP, '<strong>These are your search results<script>alert(1)</script></strong>', null, {escape: false})
  56. ).toEqual('<strong>These are your search results</strong>');
  57. });
  58. it('keeps old texts when registering existing bundle', function() {
  59. OC.L10N.register(TEST_APP, {
  60. 'sunny': 'sonnig',
  61. 'new': 'neu'
  62. });
  63. expect(t(TEST_APP, 'sunny')).toEqual('sonnig');
  64. expect(t(TEST_APP, 'new')).toEqual('neu');
  65. });
  66. });
  67. describe('plurals', function() {
  68. function checkPlurals() {
  69. expect(
  70. n(TEST_APP, 'download %n file', 'download %n files', 0)
  71. ).toEqual('0 Dateien herunterladen');
  72. expect(
  73. n(TEST_APP, 'download %n file', 'download %n files', 1)
  74. ).toEqual('1 Datei herunterladen');
  75. expect(
  76. n(TEST_APP, 'download %n file', 'download %n files', 2)
  77. ).toEqual('2 Dateien herunterladen');
  78. expect(
  79. n(TEST_APP, 'download %n file', 'download %n files', 1024)
  80. ).toEqual('1024 Dateien herunterladen');
  81. }
  82. it('generates plural for default text when translation does not exist', function() {
  83. spyOn(console, 'warn');
  84. OC.L10N.register(TEST_APP, {
  85. });
  86. expect(
  87. n(TEST_APP, 'download %n file', 'download %n files', 0)
  88. ).toEqual('download 0 files');
  89. expect(
  90. n(TEST_APP, 'download %n file', 'download %n files', 1)
  91. ).toEqual('download 1 file');
  92. expect(
  93. n(TEST_APP, 'download %n file', 'download %n files', 2)
  94. ).toEqual('download 2 files');
  95. expect(
  96. n(TEST_APP, 'download %n file', 'download %n files', 1024)
  97. ).toEqual('download 1024 files');
  98. });
  99. it('generates plural with default function when no forms specified', function() {
  100. spyOn(console, 'warn');
  101. OC.L10N.register(TEST_APP, {
  102. '_download %n file_::_download %n files_':
  103. ['%n Datei herunterladen', '%n Dateien herunterladen']
  104. });
  105. checkPlurals();
  106. });
  107. });
  108. describe('async loading of translations', function() {
  109. afterEach(() => {
  110. document.documentElement.removeAttribute('data-locale')
  111. })
  112. it('loads bundle for given app and calls callback', function(done) {
  113. document.documentElement.setAttribute('data-locale', 'zh_CN')
  114. var callbackStub = sinon.stub();
  115. var promiseStub = sinon.stub();
  116. var loading = OC.L10N.load(TEST_APP, callbackStub);
  117. expect(callbackStub.notCalled).toEqual(true);
  118. var req = fakeServer.requests[0];
  119. loading
  120. .then(promiseStub)
  121. .then(function() {
  122. expect(fakeServer.requests.length).toEqual(1);
  123. expect(req.url).toEqual(
  124. OC.getRootPath() + '/apps3/' + TEST_APP + '/l10n/zh_CN.json'
  125. );
  126. expect(callbackStub.calledOnce).toEqual(true);
  127. expect(promiseStub.calledOnce).toEqual(true);
  128. expect(t(TEST_APP, 'Hello world!')).toEqual('你好世界!');
  129. })
  130. .then(done)
  131. .catch(e => expect(e).toBe('No error expected!'));
  132. expect(promiseStub.notCalled).toEqual(true);
  133. req.respond(
  134. 200,
  135. { 'Content-Type': 'application/json' },
  136. JSON.stringify({
  137. translations: {'Hello world!': '你好世界!'},
  138. pluralForm: 'nplurals=2; plural=(n != 1);'
  139. })
  140. );
  141. });
  142. it('calls callback if translation already available', function(done) {
  143. var callbackStub = sinon.stub();
  144. spyOn(console, 'warn');
  145. OC.L10N.register(TEST_APP, {
  146. 'Hello world!': 'Hallo Welt!'
  147. });
  148. OC.L10N.load(TEST_APP, callbackStub)
  149. .then(function() {
  150. expect(callbackStub.calledOnce).toEqual(true);
  151. expect(fakeServer.requests.length).toEqual(0);
  152. })
  153. .then(done);
  154. });
  155. it('calls callback if locale is en', function(done) {
  156. var callbackStub = sinon.stub();
  157. OC.L10N.load(TEST_APP, callbackStub)
  158. .then(function() {
  159. expect(callbackStub.calledOnce).toEqual(true);
  160. expect(fakeServer.requests.length).toEqual(0);
  161. })
  162. .then(done)
  163. .catch(done);
  164. });
  165. });
  166. });