i18n.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. QUnit.test('i18n .detectBrowserLocale', assert => {
  2. var original_userLanguage = window.navigator.userLanguage
  3. var original_language = window.navigator.language
  4. var original_languages = window.navigator.languages
  5. var reset = function(){
  6. window.navigator.userLanguage = original_userLanguage
  7. window.navigator.language = original_language
  8. window.navigator.languages = original_languages
  9. }
  10. var mock = function(languages, language, userLanguage){
  11. Object.defineProperty(window.navigator, 'language', {value: language, configurable: true });
  12. Object.defineProperty(window.navigator, 'languages', {value: languages, configurable: true });
  13. Object.defineProperty(window.navigator, 'userLanguage', {value: userLanguage, configurable: true });
  14. }
  15. mock(undefined, undefined, undefined)
  16. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  17. mock(undefined, undefined, 'non-existant');
  18. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  19. mock(undefined, undefined, 'en');
  20. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  21. mock(undefined, undefined, 'lt');
  22. assert.equal(App.i18n.detectBrowserLocale(), 'lt')
  23. mock(undefined, 'lt', 'lv');
  24. assert.equal(App.i18n.detectBrowserLocale(), 'lt')
  25. mock(undefined, 'en', 'lv');
  26. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  27. mock(['en'], 'lt', 'lv');
  28. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  29. mock(['en-us'], 'lt', 'lv');
  30. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  31. mock(['en-US'], 'lt', 'lv');
  32. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  33. mock(['lt', 'en-US'], 'lt', 'lv');
  34. assert.equal(App.i18n.detectBrowserLocale(), 'lt')
  35. mock(['en-GB', 'en-US'], 'lt', 'lv');
  36. assert.equal(App.i18n.detectBrowserLocale(), 'en-gb')
  37. mock(['en-XYZ'], 'lt', 'lv');
  38. assert.equal(App.i18n.detectBrowserLocale(), 'en-us')
  39. mock(['xyz', 'lt'], 'lv', undefined);
  40. assert.equal(App.i18n.detectBrowserLocale(), 'lt')
  41. reset()
  42. })
  43. // i18n
  44. QUnit.test('i18n', assert => {
  45. // de
  46. App.i18n.set('de-de')
  47. var translated = App.i18n.translateContent('yes')
  48. assert.equal(translated, 'ja', 'de-de - yes / ja translated correctly')
  49. translated = App.i18n.translatePlain('yes')
  50. assert.equal(translated, 'ja', 'de-de - yes / ja translated correctly')
  51. translated = App.i18n.translateInline('yes')
  52. assert.equal(translated, 'ja', 'de-de - yes / ja translated correctly')
  53. translated = App.i18n.translateDeep({
  54. days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  55. today: 'today',
  56. })
  57. assert.deepEqual(translated, {
  58. days: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
  59. today: 'heute',
  60. }, 'de-de - deep object/array translated correctly')
  61. translated = App.i18n.translateDeepPlain({
  62. days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  63. today: 'today',
  64. })
  65. assert.deepEqual(translated, {
  66. days: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
  67. today: 'heute',
  68. }, 'de-de - deep object/array translated correctly')
  69. translated = App.i18n.translateContent('%s ago', 123);
  70. assert.equal(translated, 'vor 123', 'de-de - %s')
  71. translated = App.i18n.translateContent('%s ago', '<b>quote</b>')
  72. assert.equal(translated, 'vor &lt;b&gt;quote&lt;/b&gt;', 'de-de - %s - quote')
  73. translated = App.i18n.translateContent('%s %s test', 123, 'xxx |B|')
  74. assert.equal(translated, '123 xxx |B| test', 'de-de - %s %s')
  75. translated = App.i18n.translateContent('|%s| %s test', 123, 'xxx')
  76. assert.equal(translated, '<b>123</b> xxx test', 'de-de - *%s* %s')
  77. translated = App.i18n.translateContent('||%s|| %s test', 123, 'xxx')
  78. assert.equal(translated, '<i>123</i> xxx test', 'de-de - *%s* %s')
  79. translated = App.i18n.translateContent('_%s_ %s test', 123, 'xxx')
  80. assert.equal(translated, '<u>123</u> xxx test', 'de-de - _%s_ %s')
  81. translated = App.i18n.translateContent('§%s§ %s test', 123, 'xxx')
  82. assert.equal(translated, '<kbd>123</kbd> xxx test', 'de-de - §%s§ %s')
  83. translated = App.i18n.translateContent('//%s// %s test', 123, 'xxx')
  84. assert.equal(translated, '<del>123</del> xxx test', 'de-de - //%s// %s')
  85. translated = App.i18n.translateContent('\'%s\' %s test', 123, 'xxx')
  86. assert.equal(translated, '&#39;123&#39; xxx test', 'de-de - \'%s\' %s')
  87. translated = App.i18n.translateContent('<test&now>//*äöüß')
  88. assert.equal(translated, '&lt;test&amp;now&gt;//*äöüß', 'de - <test&now>//*äöüß')
  89. translated = App.i18n.translateContent('some link [to what ever](http://lalala)')
  90. assert.equal(translated, 'some link <a href="http://lalala" target="_blank">to what ever</a>', 'de-de - link')
  91. translated = App.i18n.translateContent('some link [to what ever](%s)', 'http://lalala')
  92. assert.equal(translated, 'some link <a href="http://lalala" target="_blank">to what ever</a>', 'de-de - link')
  93. translated = App.i18n.translateContent('Enables user authentication via %s. Register your app first at [%s](%s).', 'XXX', 'YYY', 'http://lalala')
  94. assert.equal(translated, 'Aktivieren der Benutzeranmeldung über XXX. Registrieren Sie Ihre Anwendung zuerst über <a href="http://lalala" target="_blank">YYY</a>.', 'en-us - link')
  95. var time_local = new Date();
  96. var offset = time_local.getTimezoneOffset();
  97. var timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z', offset);
  98. assert.equal(timestamp, '06.11.2012 21:07', 'de-de - timestamp translated correctly')
  99. var timestamp = App.i18n.translateTimestamp('2021-02-08 09:13:20 UTC', offset);
  100. assert.equal(timestamp, '08.02.2021 09:13', 'de-de - timestamp translated correctly with UTC format')
  101. timestamp = App.i18n.translateTimestamp('', offset);
  102. assert.equal(timestamp, '', 'de-de - timestamp translated correctly')
  103. timestamp = App.i18n.translateTimestamp(null, offset);
  104. assert.equal(timestamp, null, 'de-de - timestamp translated correctly')
  105. timestamp = App.i18n.translateTimestamp(undefined, offset);
  106. assert.equal(timestamp, undefined, 'de-de - timestamp translated correctly')
  107. var date = App.i18n.translateDate('2012-11-06', false)
  108. assert.equal(date, '06.11.2012', 'de-de - date translated correctly')
  109. date = App.i18n.translateDate('', false)
  110. assert.equal(date, '', 'de-de - date translated correctly')
  111. date = App.i18n.translateDate(null, false)
  112. assert.equal(date, null, 'de-de - date translated correctly')
  113. date = App.i18n.translateDate(undefined, false)
  114. assert.equal(date, undefined, 'de-de - date translated correctly')
  115. date = App.i18n.timeFormat()
  116. assert.deepEqual(date, { "FORMAT_DATE": "dd.mm.yyyy", "FORMAT_DATETIME": "dd.mm.yyyy HH:MM" }, 'timeFormat property')
  117. // Verify that the datepicker gets the correct format too.
  118. el_date = App.UiElement.date.render({name: 'test', value: '2018-07-06'})
  119. assert.equal(el_date.find('.js-datepicker').get(0).value, '06.07.2018')
  120. // en
  121. App.i18n.set('en-us')
  122. translated = App.i18n.translateContent('yes')
  123. assert.equal(translated, 'yes', 'en-us - yes translated correctly')
  124. translated = App.i18n.translatePlain('yes')
  125. assert.equal(translated, 'yes', 'en-us - yes translated correctly')
  126. translated = App.i18n.translateInline('yes')
  127. assert.equal(translated, 'yes', 'en-us - yes translated correctly')
  128. translated = App.i18n.translateContent('%s ago', 123);
  129. assert.equal(translated, '123 ago', 'en-us - %s')
  130. translated = App.i18n.translateContent('%s ago', '<b>quote</b>')
  131. assert.equal(translated, '&lt;b&gt;quote&lt;/b&gt; ago', 'en-us - %s - qupte')
  132. translated = App.i18n.translateContent('%s %s test', 123, 'xxx')
  133. assert.equal(translated, '123 xxx test', 'en-us - %s %s')
  134. translated = App.i18n.translateContent('|%s| %s test', 123, 'xxx |B|')
  135. assert.equal(translated, '<b>123</b> xxx |B| test', 'en-us - *%s* %s')
  136. translated = App.i18n.translateContent('||%s|| %s test', 123, 'xxx')
  137. assert.equal(translated, '<i>123</i> xxx test', 'en-us - *%s* %s')
  138. translated = App.i18n.translateContent('_%s_ %s test', 123, 'xxx')
  139. assert.equal(translated, '<u>123</u> xxx test', 'en-us - _%s_ %s')
  140. translated = App.i18n.translateContent('§%s§ %s test', 123, 'xxx')
  141. assert.equal(translated, '<kbd>123</kbd> xxx test', 'en-us - §%s§ %s')
  142. translated = App.i18n.translateContent('Here you can search for tickets, customers, and organizations. Use the asterisk §*§ to find anything, e.g. §smi*§ or §rosent*l§. You also can use ||quotation marks|| for searching phrases: §"some phrase"§.')
  143. assert.equal(translated, 'Here you can search for tickets, customers, and organizations. Use the asterisk <kbd>*</kbd> to find anything, e.g. <kbd>smi*</kbd> or <kbd>rosent*l</kbd>. You also can use <i>quotation marks</i> for searching phrases: <kbd>&quot;some phrase&quot;</kbd>.', 'en-us - §§ §§ §§ || §§')
  144. translated = App.i18n.translateContent('//%s// %s test', 123, 'xxx')
  145. assert.equal(translated, '<del>123</del> xxx test', 'en-us - //%s// %s')
  146. translated = App.i18n.translateContent('\'%s\' %s test', 123, 'xxx')
  147. assert.equal(translated, '&#39;123&#39; xxx test', 'en-us - \'%s\' %s')
  148. translated = App.i18n.translateContent('<test&now>')
  149. assert.equal(translated, '&lt;test&amp;now&gt;', 'en-us - <test&now>')
  150. translated = App.i18n.translateContent('some link [to what ever](http://lalala)')
  151. assert.equal(translated, 'some link <a href="http://lalala" target="_blank">to what ever</a>', 'en-us - link')
  152. translated = App.i18n.translateContent('some link [to what ever](%s)', 'http://lalala')
  153. assert.equal(translated, 'some link <a href="http://lalala" target="_blank">to what ever</a>', 'en-us - link')
  154. translated = App.i18n.translateContent('Enables user authentication via %s. Register your app first at [%s](%s).', 'XXX', 'YYY', 'http://lalala')
  155. assert.equal(translated, 'Enables user authentication via XXX. Register your app first at <a href="http://lalala" target="_blank">YYY</a>.', 'en-us - link')
  156. timestamp = App.i18n.translateTimestamp('2012-11-06T09:07:24Z', offset)
  157. assert.equal(timestamp, '11/06/2012 9:07 am', 'en - timestamp translated correctly (pm)')
  158. timestamp = App.i18n.translateTimestamp('2012-11-06T22:07:24Z', offset)
  159. assert.equal(timestamp, '11/06/2012 10:07 pm', 'en - timestamp translated correctly (pm)')
  160. timestamp = App.i18n.translateTimestamp('', offset);
  161. assert.equal(timestamp, '', 'en - timestamp translated correctly')
  162. timestamp = App.i18n.translateTimestamp(null, offset);
  163. assert.equal(timestamp, null, 'en - timestamp translated correctly')
  164. timestamp = App.i18n.translateTimestamp(undefined, offset);
  165. assert.equal(timestamp, undefined, 'en - timestamp translated correctly')
  166. date = App.i18n.translateDate('2012-11-06', false)
  167. assert.equal(date, '11/06/2012', 'en - date translated correctly')
  168. date = App.i18n.translateDate('', false)
  169. assert.equal(date, '', 'en - date translated correctly')
  170. date = App.i18n.translateDate(null, false)
  171. assert.equal(date, null, 'en - date translated correctly')
  172. date = App.i18n.translateDate(undefined, false)
  173. assert.equal(date, undefined, 'en - date translated correctly')
  174. date = App.i18n.timeFormat()
  175. assert.deepEqual(date, { "FORMAT_DATE": "mm/dd/yyyy", "FORMAT_DATETIME": "mm/dd/yyyy l:MM P" }, 'timeFormat property')
  176. // Verify that the datepicker gets the correct format too.
  177. el_date = App.UiElement.date.render({name: 'test', value: '2018-07-06'})
  178. assert.equal(el_date.find('.js-datepicker').get(0).value, '07/06/2018')
  179. // locale alias test
  180. // de
  181. App.i18n.set('de')
  182. var translated = App.i18n.translateContent('yes')
  183. assert.equal(translated, 'ja', 'de - yes / ja translated correctly')
  184. // locale detection test
  185. // de-ch
  186. App.i18n.set('de-ch')
  187. var translated = App.i18n.translateContent('yes')
  188. assert.equal(translated, 'ja', 'de - yes / ja translated correctly')
  189. });