L10nTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright (c) 2016 Joas Schilling <nickvergessen@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\L10N;
  9. use DateTime;
  10. use OC\L10N\Factory;
  11. use OC\L10N\L10N;
  12. use OCP\IConfig;
  13. use OCP\IRequest;
  14. use OCP\IUserSession;
  15. use Test\TestCase;
  16. /**
  17. * Class L10nTest
  18. *
  19. * @package Test\L10N
  20. */
  21. class L10nTest extends TestCase {
  22. /**
  23. * @return Factory
  24. */
  25. protected function getFactory() {
  26. /** @var \OCP\IConfig $config */
  27. $config = $this->createMock(IConfig::class);
  28. /** @var \OCP\IRequest $request */
  29. $request = $this->createMock(IRequest::class);
  30. /** @var IUserSession $userSession */
  31. $userSession = $this->createMock(IUserSession::class);
  32. return new Factory($config, $request, $userSession, \OC::$SERVERROOT);
  33. }
  34. public function testGermanPluralTranslations() {
  35. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  36. $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
  37. $this->assertEquals('1 Datei', (string) $l->n('%n file', '%n files', 1));
  38. $this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
  39. }
  40. public function testRussianPluralTranslations() {
  41. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json';
  42. $l = new L10N($this->getFactory(), 'test', 'ru', 'ru_UA',[$transFile]);
  43. $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1));
  44. $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2));
  45. $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6));
  46. $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21));
  47. $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22));
  48. $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26));
  49. /*
  50. 1 file 1 файл 1 папка
  51. 2-4 files 2-4 файла 2-4 папки
  52. 5-20 files 5-20 файлов 5-20 папок
  53. 21 files 21 файл 21 папка
  54. 22-24 files 22-24 файла 22-24 папки
  55. 25-30 files 25-30 файлов 25-30 папок
  56. etc
  57. 100 files 100 файлов, 100 папок
  58. 1000 files 1000 файлов 1000 папок
  59. */
  60. }
  61. public function testCzechPluralTranslations() {
  62. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/cs.json';
  63. $l = new L10N($this->getFactory(), 'test', 'cs', 'cs_CZ', [$transFile]);
  64. $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1));
  65. $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2));
  66. $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
  67. }
  68. public function localizationData() {
  69. return [
  70. // timestamp as string
  71. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', '1234567890'],
  72. ['13. Februar 2009 um 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', '1234567890'],
  73. ['February 13, 2009', 'en', 'en_US', 'date', '1234567890'],
  74. ['13. Februar 2009', 'de', 'de_DE', 'date', '1234567890'],
  75. ['11:31:30 PM GMT+0', 'en', 'en_US', 'time', '1234567890'],
  76. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', '1234567890'],
  77. // timestamp as int
  78. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', 1234567890],
  79. ['13. Februar 2009 um 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', 1234567890],
  80. ['February 13, 2009', 'en', 'en_US', 'date', 1234567890],
  81. ['13. Februar 2009', 'de', 'de_DE', 'date', 1234567890],
  82. ['11:31:30 PM GMT+0', 'en', 'en_US', 'time', 1234567890],
  83. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', 1234567890],
  84. // DateTime object
  85. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', new DateTime('@1234567890')],
  86. ['13. Februar 2009 um 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', new DateTime('@1234567890')],
  87. ['February 13, 2009', 'en', 'en_US', 'date', new DateTime('@1234567890')],
  88. ['13. Februar 2009', 'de', 'de_DE', 'date', new DateTime('@1234567890')],
  89. ['11:31:30 PM GMT+0', 'en', 'en_US', 'time', new DateTime('@1234567890')],
  90. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTime('@1234567890')],
  91. // en_GB
  92. ['13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  93. ['13 February 2009', 'en_GB', 'en_GB', 'date', new DateTime('@1234567890')],
  94. ['23:31:30 GMT+0', 'en_GB', 'en_GB', 'time', new DateTime('@1234567890')],
  95. ['13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  96. ['13 February 2009', 'en-GB', 'en_GB', 'date', new DateTime('@1234567890')],
  97. ['23:31:30 GMT+0', 'en-GB', 'en_GB', 'time', new DateTime('@1234567890')],
  98. ];
  99. }
  100. /**
  101. * @dataProvider localizationData
  102. */
  103. public function testNumericStringLocalization($expectedDate, $lang, $locale, $type, $value) {
  104. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  105. $this->assertSame($expectedDate, $l->l($type, $value));
  106. }
  107. public function firstDayData() {
  108. return [
  109. [1, 'de', 'de_DE'],
  110. [0, 'en', 'en_US'],
  111. ];
  112. }
  113. /**
  114. * @dataProvider firstDayData
  115. * @param $expected
  116. * @param $lang
  117. * @param $locale
  118. */
  119. public function testFirstWeekDay($expected, $lang, $locale) {
  120. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  121. $this->assertSame($expected, $l->l('firstday', 'firstday'));
  122. }
  123. public function jsDateData() {
  124. return [
  125. ['dd.MM.yy', 'de', 'de_DE'],
  126. ['M/d/yy', 'en', 'en_US'],
  127. ];
  128. }
  129. /**
  130. * @dataProvider jsDateData
  131. * @param $expected
  132. * @param $lang
  133. * @param $locale
  134. */
  135. public function testJSDate($expected, $lang, $locale) {
  136. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  137. $this->assertSame($expected, $l->l('jsdate', 'jsdate'));
  138. }
  139. public function testFactoryGetLanguageCode() {
  140. $l = $this->getFactory()->get('lib', 'de');
  141. $this->assertEquals('de', $l->getLanguageCode());
  142. }
  143. public function testServiceGetLanguageCode() {
  144. $l = \OC::$server->getL10N('lib', 'de');
  145. $this->assertEquals('de', $l->getLanguageCode());
  146. }
  147. public function testWeekdayName() {
  148. $l = \OC::$server->getL10N('lib', 'de');
  149. $this->assertEquals('Mo.', $l->l('weekdayName', new \DateTime('2017-11-6'), ['width' => 'abbreviated']));
  150. }
  151. /**
  152. * @dataProvider findLanguageFromLocaleData
  153. * @param $locale
  154. * @param $language
  155. */
  156. public function testFindLanguageFromLocale($locale, $language) {
  157. $this->assertEquals(
  158. $language,
  159. \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale)
  160. );
  161. }
  162. /**
  163. * @return array
  164. */
  165. public function findLanguageFromLocaleData(): array {
  166. return [
  167. 'en_US' => ['en_US', 'en'],
  168. 'en_UK' => ['en_UK', 'en'],
  169. 'de_DE' => ['de_DE', 'de_DE'],
  170. 'de_AT' => ['de_AT', 'de'],
  171. 'es_EC' => ['es_EC', 'es_EC'],
  172. 'fi_FI' => ['fi_FI', 'fi'],
  173. 'zh_CN' => ['zh_CN', 'zh_CN'],
  174. ];
  175. }
  176. }