L10nTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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', [$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', [$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', [$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 array(
  70. // timestamp as string
  71. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', '1234567890'),
  72. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', '1234567890'),
  73. array('February 13, 2009', 'en', 'date', '1234567890'),
  74. array('13. Februar 2009', 'de', 'date', '1234567890'),
  75. array('11:31:30 PM GMT+0', 'en', 'time', '1234567890'),
  76. array('23:31:30 GMT+0', 'de', 'time', '1234567890'),
  77. // timestamp as int
  78. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', 1234567890),
  79. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', 1234567890),
  80. array('February 13, 2009', 'en', 'date', 1234567890),
  81. array('13. Februar 2009', 'de', 'date', 1234567890),
  82. array('11:31:30 PM GMT+0', 'en', 'time', 1234567890),
  83. array('23:31:30 GMT+0', 'de', 'time', 1234567890),
  84. // DateTime object
  85. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', new DateTime('@1234567890')),
  86. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', new DateTime('@1234567890')),
  87. array('February 13, 2009', 'en', 'date', new DateTime('@1234567890')),
  88. array('13. Februar 2009', 'de', 'date', new DateTime('@1234567890')),
  89. array('11:31:30 PM GMT+0', 'en', 'time', new DateTime('@1234567890')),
  90. array('23:31:30 GMT+0', 'de', 'time', new DateTime('@1234567890')),
  91. // en_GB
  92. array('13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'datetime', new DateTime('@1234567890')),
  93. array('13 February 2009', 'en_GB', 'date', new DateTime('@1234567890')),
  94. array('23:31:30 GMT+0', 'en_GB', 'time', new DateTime('@1234567890')),
  95. array('13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'datetime', new DateTime('@1234567890')),
  96. array('13 February 2009', 'en-GB', 'date', new DateTime('@1234567890')),
  97. array('23:31:30 GMT+0', 'en-GB', 'time', new DateTime('@1234567890')),
  98. );
  99. }
  100. /**
  101. * @dataProvider localizationData
  102. */
  103. public function testNumericStringLocalization($expectedDate, $lang, $type, $value) {
  104. $l = new L10N($this->getFactory(), 'test', $lang, []);
  105. $this->assertSame($expectedDate, $l->l($type, $value));
  106. }
  107. public function firstDayData() {
  108. return array(
  109. array(1, 'de'),
  110. array(0, 'en'),
  111. );
  112. }
  113. /**
  114. * @dataProvider firstDayData
  115. * @param $expected
  116. * @param $lang
  117. */
  118. public function testFirstWeekDay($expected, $lang) {
  119. $l = new L10N($this->getFactory(), 'test', $lang, []);
  120. $this->assertSame($expected, $l->l('firstday', 'firstday'));
  121. }
  122. public function jsDateData() {
  123. return array(
  124. array('dd.MM.yy', 'de'),
  125. array('M/d/yy', 'en'),
  126. );
  127. }
  128. /**
  129. * @dataProvider jsDateData
  130. * @param $expected
  131. * @param $lang
  132. */
  133. public function testJSDate($expected, $lang) {
  134. $l = new L10N($this->getFactory(), 'test', $lang, []);
  135. $this->assertSame($expected, $l->l('jsdate', 'jsdate'));
  136. }
  137. public function testFactoryGetLanguageCode() {
  138. $l = $this->getFactory()->get('lib', 'de');
  139. $this->assertEquals('de', $l->getLanguageCode());
  140. }
  141. public function testServiceGetLanguageCode() {
  142. $l = \OC::$server->getL10N('lib', 'de');
  143. $this->assertEquals('de', $l->getLanguageCode());
  144. }
  145. public function testWeekdayName() {
  146. $l = \OC::$server->getL10N('lib', 'de');
  147. $this->assertEquals('Mo.', $l->l('weekdayName', new \DateTime('2017-11-6'), ['width' => 'abbreviated']));
  148. }
  149. }