1
0

l10nSpec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. OC.appswebroots[TEST_APP] = OC.getRootPath() + '/apps3/jsunittestapp';
  14. });
  15. afterEach(function() {
  16. OC.L10N._unregister(TEST_APP);
  17. delete 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. it('loads bundle for given app and calls callback', function(done) {
  110. var localeStub = sinon.stub(OC, 'getLocale').returns('zh_CN');
  111. var callbackStub = sinon.stub();
  112. var promiseStub = sinon.stub();
  113. var loading = OC.L10N.load(TEST_APP, callbackStub);
  114. expect(callbackStub.notCalled).toEqual(true);
  115. var req = fakeServer.requests[0];
  116. loading
  117. .then(promiseStub)
  118. .then(function() {
  119. expect(fakeServer.requests.length).toEqual(1);
  120. expect(req.url).toEqual(
  121. OC.getRootPath() + '/apps3/' + TEST_APP + '/l10n/zh_CN.json'
  122. );
  123. expect(callbackStub.calledOnce).toEqual(true);
  124. expect(promiseStub.calledOnce).toEqual(true);
  125. expect(t(TEST_APP, 'Hello world!')).toEqual('你好世界!');
  126. localeStub.restore();
  127. })
  128. .then(done);
  129. expect(promiseStub.notCalled).toEqual(true);
  130. req.respond(
  131. 200,
  132. { 'Content-Type': 'application/json' },
  133. JSON.stringify({
  134. translations: {'Hello world!': '你好世界!'},
  135. pluralForm: 'nplurals=2; plural=(n != 1);'
  136. })
  137. );
  138. });
  139. it('calls callback if translation already available', function(done) {
  140. var callbackStub = sinon.stub();
  141. spyOn(console, 'warn');
  142. OC.L10N.register(TEST_APP, {
  143. 'Hello world!': 'Hallo Welt!'
  144. });
  145. OC.L10N.load(TEST_APP, callbackStub)
  146. .then(function() {
  147. expect(callbackStub.calledOnce).toEqual(true);
  148. expect(fakeServer.requests.length).toEqual(0);
  149. })
  150. .then(done);
  151. });
  152. it('calls callback if locale is en', function(done) {
  153. var callbackStub = sinon.stub();
  154. OC.L10N.load(TEST_APP, callbackStub)
  155. .then(function() {
  156. expect(callbackStub.calledOnce).toEqual(true);
  157. expect(fakeServer.requests.length).toEqual(0);
  158. })
  159. .then(done)
  160. .catch(done);
  161. });
  162. });
  163. });