l10nSpec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.webroot + '/apps3/jsunittestapp';
  14. });
  15. afterEach(function() {
  16. delete OC.L10N._bundles[TEST_APP];
  17. delete OC.appswebroots[TEST_APP];
  18. });
  19. describe('text translation', function() {
  20. beforeEach(function() {
  21. OC.L10N.register(TEST_APP, {
  22. 'Hello world!': 'Hallo Welt!',
  23. 'Hello {name}, the weather is {weather}': 'Hallo {name}, das Wetter ist {weather}',
  24. 'sunny': 'sonnig'
  25. });
  26. });
  27. it('returns untranslated text when no bundle exists', function() {
  28. delete OC.L10N._bundles[TEST_APP];
  29. expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
  30. });
  31. it('returns untranslated text when no key exists', function() {
  32. expect(t(TEST_APP, 'unknown text')).toEqual('unknown text');
  33. });
  34. it('returns translated text when key exists', function() {
  35. expect(t(TEST_APP, 'Hello world!')).toEqual('Hallo Welt!');
  36. });
  37. it('returns translated text with placeholder', function() {
  38. expect(
  39. t(TEST_APP, 'Hello {name}, the weather is {weather}', {name: 'Steve', weather: t(TEST_APP, 'sunny')})
  40. ).toEqual('Hallo Steve, das Wetter ist sonnig');
  41. });
  42. it('returns text with escaped placeholder', function() {
  43. expect(
  44. t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'})
  45. ).toEqual('Hello &lt;strong&gt;Steve&lt;/strong&gt;');
  46. });
  47. it('returns text with not escaped placeholder', function() {
  48. expect(
  49. t(TEST_APP, 'Hello {name}', {name: '<strong>Steve</strong>'}, null, {escape: false})
  50. ).toEqual('Hello <strong>Steve</strong>');
  51. });
  52. it('keeps old texts when registering existing bundle', function() {
  53. OC.L10N.register(TEST_APP, {
  54. 'sunny': 'sonnig',
  55. 'new': 'neu'
  56. });
  57. expect(t(TEST_APP, 'sunny')).toEqual('sonnig');
  58. expect(t(TEST_APP, 'new')).toEqual('neu');
  59. });
  60. });
  61. describe('plurals', function() {
  62. function checkPlurals() {
  63. expect(
  64. n(TEST_APP, 'download %n file', 'download %n files', 0)
  65. ).toEqual('0 Dateien herunterladen');
  66. expect(
  67. n(TEST_APP, 'download %n file', 'download %n files', 1)
  68. ).toEqual('1 Datei herunterladen');
  69. expect(
  70. n(TEST_APP, 'download %n file', 'download %n files', 2)
  71. ).toEqual('2 Dateien herunterladen');
  72. expect(
  73. n(TEST_APP, 'download %n file', 'download %n files', 1024)
  74. ).toEqual('1024 Dateien herunterladen');
  75. }
  76. it('generates plural for default text when translation does not exist', function() {
  77. OC.L10N.register(TEST_APP, {
  78. });
  79. expect(
  80. n(TEST_APP, 'download %n file', 'download %n files', 0)
  81. ).toEqual('download 0 files');
  82. expect(
  83. n(TEST_APP, 'download %n file', 'download %n files', 1)
  84. ).toEqual('download 1 file');
  85. expect(
  86. n(TEST_APP, 'download %n file', 'download %n files', 2)
  87. ).toEqual('download 2 files');
  88. expect(
  89. n(TEST_APP, 'download %n file', 'download %n files', 1024)
  90. ).toEqual('download 1024 files');
  91. });
  92. it('generates plural with default function when no forms specified', function() {
  93. OC.L10N.register(TEST_APP, {
  94. '_download %n file_::_download %n files_':
  95. ['%n Datei herunterladen', '%n Dateien herunterladen']
  96. });
  97. checkPlurals();
  98. });
  99. it('generates plural with generated function when forms is specified', function() {
  100. OC.L10N.register(TEST_APP, {
  101. '_download %n file_::_download %n files_':
  102. ['%n Datei herunterladen', '%n Dateien herunterladen']
  103. }, 'nplurals=2; plural=(n != 1);');
  104. checkPlurals();
  105. });
  106. it('generates plural with function when forms is specified as function', function() {
  107. OC.L10N.register(TEST_APP, {
  108. '_download %n file_::_download %n files_':
  109. ['%n Datei herunterladen', '%n Dateien herunterladen']
  110. }, function(n) {
  111. return {
  112. nplurals: 2,
  113. plural: (n !== 1) ? 1 : 0
  114. };
  115. });
  116. checkPlurals();
  117. });
  118. });
  119. describe('async loading of translations', function() {
  120. it('loads bundle for given app and calls callback', function() {
  121. var localeStub = sinon.stub(OC, 'getLocale').returns('zh_CN');
  122. var callbackStub = sinon.stub();
  123. var promiseStub = sinon.stub();
  124. OC.L10N.load(TEST_APP, callbackStub).then(promiseStub);
  125. expect(callbackStub.notCalled).toEqual(true);
  126. expect(promiseStub.notCalled).toEqual(true);
  127. expect(fakeServer.requests.length).toEqual(1);
  128. var req = fakeServer.requests[0];
  129. expect(req.url).toEqual(
  130. OC.webroot + '/apps3/' + TEST_APP + '/l10n/zh_CN.json'
  131. );
  132. req.respond(
  133. 200,
  134. { 'Content-Type': 'application/json' },
  135. JSON.stringify({
  136. translations: {'Hello world!': '你好世界!'},
  137. pluralForm: 'nplurals=2; plural=(n != 1);'
  138. })
  139. );
  140. expect(callbackStub.calledOnce).toEqual(true);
  141. expect(promiseStub.calledOnce).toEqual(true);
  142. expect(t(TEST_APP, 'Hello world!')).toEqual('你好世界!');
  143. localeStub.restore();
  144. });
  145. it('calls callback if translation already available', function() {
  146. var promiseStub = sinon.stub();
  147. var callbackStub = sinon.stub();
  148. OC.L10N.register(TEST_APP, {
  149. 'Hello world!': 'Hallo Welt!'
  150. });
  151. OC.L10N.load(TEST_APP, callbackStub).then(promiseStub);
  152. expect(callbackStub.calledOnce).toEqual(true);
  153. expect(promiseStub.calledOnce).toEqual(true);
  154. expect(fakeServer.requests.length).toEqual(0);
  155. });
  156. it('calls callback if locale is en', function() {
  157. var localeStub = sinon.stub(OC, 'getLocale').returns('en');
  158. var promiseStub = sinon.stub();
  159. var callbackStub = sinon.stub();
  160. OC.L10N.load(TEST_APP, callbackStub).then(promiseStub);
  161. expect(callbackStub.calledOnce).toEqual(true);
  162. expect(promiseStub.calledOnce).toEqual(true);
  163. expect(fakeServer.requests.length).toEqual(0);
  164. });
  165. });
  166. });