L10nTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\ICacheFactory;
  13. use OCP\IConfig;
  14. use OCP\IRequest;
  15. use OCP\IUserSession;
  16. use Test\TestCase;
  17. /**
  18. * Class L10nTest
  19. *
  20. * @package Test\L10N
  21. */
  22. class L10nTest extends TestCase {
  23. /**
  24. * @return Factory
  25. */
  26. protected function getFactory() {
  27. /** @var \OCP\IConfig $config */
  28. $config = $this->createMock(IConfig::class);
  29. /** @var \OCP\IRequest $request */
  30. $request = $this->createMock(IRequest::class);
  31. /** @var IUserSession $userSession */
  32. $userSession = $this->createMock(IUserSession::class);
  33. $cacheFactory = $this->createMock(ICacheFactory::class);
  34. return new Factory($config, $request, $userSession, $cacheFactory, \OC::$SERVERROOT);
  35. }
  36. public function testSimpleTranslationWithTrailingColon(): void {
  37. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  38. $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
  39. $this->assertEquals('Files:', $l->t('Files:'));
  40. }
  41. public function testGermanPluralTranslations() {
  42. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  43. $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
  44. $this->assertEquals('1 Datei', (string) $l->n('%n file', '%n files', 1));
  45. $this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
  46. }
  47. public function testRussianPluralTranslations() {
  48. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/ru.json';
  49. $l = new L10N($this->getFactory(), 'test', 'ru', 'ru_UA', [$transFile]);
  50. $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1));
  51. $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2));
  52. $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6));
  53. $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21));
  54. $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22));
  55. $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26));
  56. /*
  57. 1 file 1 файл 1 папка
  58. 2-4 files 2-4 файла 2-4 папки
  59. 5-20 files 5-20 файлов 5-20 папок
  60. 21 files 21 файл 21 папка
  61. 22-24 files 22-24 файла 22-24 папки
  62. 25-30 files 25-30 файлов 25-30 папок
  63. etc
  64. 100 files 100 файлов, 100 папок
  65. 1000 files 1000 файлов 1000 папок
  66. */
  67. }
  68. public function testCzechPluralTranslations() {
  69. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/cs.json';
  70. $l = new L10N($this->getFactory(), 'test', 'cs', 'cs_CZ', [$transFile]);
  71. $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1));
  72. $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2));
  73. $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
  74. }
  75. public function testGermanPluralWithCzechLocaleTranslations() {
  76. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  77. $l = new L10N($this->getFactory(), 'test', 'de', 'cs_CZ', [$transFile]);
  78. $this->assertEquals('1 Datei', (string) $l->n('%n file', '%n files', 1));
  79. $this->assertEquals('2 Dateien', (string) $l->n('%n file', '%n files', 2));
  80. $this->assertEquals('5 Dateien', (string) $l->n('%n file', '%n files', 5));
  81. }
  82. public function dataPlaceholders(): array {
  83. return [
  84. ['Ordered placeholders one %s two %s', 'Placeholder one 1 two 2'],
  85. ['Reordered placeholders one %s two %s', 'Placeholder two 2 one 1'],
  86. ['Reordered placeholders one %1$s two %2$s', 'Placeholder two 2 one 1'],
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataPlaceholders
  91. *
  92. * @param $string
  93. * @param $expected
  94. */
  95. public function testPlaceholders($string, $expected): void {
  96. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  97. $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
  98. $this->assertEquals($expected, $l->t($string, ['1', '2']));
  99. }
  100. public function localizationData() {
  101. return [
  102. // timestamp as string
  103. ["February 13, 2009, 11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'datetime', '1234567890'],
  104. ['13. Februar 2009, 23:31:30 UTC', 'de', 'de_DE', 'datetime', '1234567890'],
  105. ['February 13, 2009', 'en', 'en_US', 'date', '1234567890'],
  106. ['13. Februar 2009', 'de', 'de_DE', 'date', '1234567890'],
  107. ["11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'time', '1234567890'],
  108. ['23:31:30 UTC', 'de', 'de_DE', 'time', '1234567890'],
  109. // timestamp as int
  110. ["February 13, 2009, 11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'datetime', 1234567890],
  111. ['13. Februar 2009, 23:31:30 UTC', 'de', 'de_DE', 'datetime', 1234567890],
  112. ['February 13, 2009', 'en', 'en_US', 'date', 1234567890],
  113. ['13. Februar 2009', 'de', 'de_DE', 'date', 1234567890],
  114. ["11:31:30\xE2\x80\xAFPM UTC", 'en', 'en_US', 'time', 1234567890],
  115. ['23:31:30 UTC', 'de', 'de_DE', 'time', 1234567890],
  116. // DateTime object
  117. ["February 13, 2009, 11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'datetime', new DateTime('@1234567890')],
  118. ['13. Februar 2009, 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', new DateTime('@1234567890')],
  119. ['February 13, 2009', 'en', 'en_US', 'date', new DateTime('@1234567890')],
  120. ['13. Februar 2009', 'de', 'de_DE', 'date', new DateTime('@1234567890')],
  121. ["11:31:30\xE2\x80\xAFPM GMT+0", 'en', 'en_US', 'time', new DateTime('@1234567890')],
  122. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTime('@1234567890')],
  123. // en_GB
  124. ['13 February 2009, 23:31:30 GMT+0', 'en_GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  125. ['13 February 2009', 'en_GB', 'en_GB', 'date', new DateTime('@1234567890')],
  126. ['23:31:30 GMT+0', 'en_GB', 'en_GB', 'time', new DateTime('@1234567890')],
  127. ['13 February 2009, 23:31:30 GMT+0', 'en-GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  128. ['13 February 2009', 'en-GB', 'en_GB', 'date', new DateTime('@1234567890')],
  129. ['23:31:30 GMT+0', 'en-GB', 'en_GB', 'time', new DateTime('@1234567890')],
  130. ];
  131. }
  132. /**
  133. * @dataProvider localizationData
  134. */
  135. public function testNumericStringLocalization($expectedDate, $lang, $locale, $type, $value) {
  136. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  137. $this->assertSame($expectedDate, $l->l($type, $value));
  138. }
  139. public function firstDayData() {
  140. return [
  141. [1, 'de', 'de_DE'],
  142. [0, 'en', 'en_US'],
  143. ];
  144. }
  145. /**
  146. * @dataProvider firstDayData
  147. * @param $expected
  148. * @param $lang
  149. * @param $locale
  150. */
  151. public function testFirstWeekDay($expected, $lang, $locale) {
  152. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  153. $this->assertSame($expected, $l->l('firstday', 'firstday'));
  154. }
  155. public function jsDateData() {
  156. return [
  157. ['dd.MM.yy', 'de', 'de_DE'],
  158. ['M/d/yy', 'en', 'en_US'],
  159. ];
  160. }
  161. /**
  162. * @dataProvider jsDateData
  163. * @param $expected
  164. * @param $lang
  165. * @param $locale
  166. */
  167. public function testJSDate($expected, $lang, $locale) {
  168. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  169. $this->assertSame($expected, $l->l('jsdate', 'jsdate'));
  170. }
  171. public function testFactoryGetLanguageCode() {
  172. $l = $this->getFactory()->get('lib', 'de');
  173. $this->assertEquals('de', $l->getLanguageCode());
  174. }
  175. public function testServiceGetLanguageCode() {
  176. $l = \OCP\Util::getL10N('lib', 'de');
  177. $this->assertEquals('de', $l->getLanguageCode());
  178. }
  179. public function testWeekdayName() {
  180. $l = \OCP\Util::getL10N('lib', 'de');
  181. $this->assertEquals('Mo.', $l->l('weekdayName', new \DateTime('2017-11-6'), ['width' => 'abbreviated']));
  182. }
  183. /**
  184. * @dataProvider findLanguageFromLocaleData
  185. * @param $locale
  186. * @param $language
  187. */
  188. public function testFindLanguageFromLocale($locale, $language) {
  189. $this->assertEquals(
  190. $language,
  191. \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale)
  192. );
  193. }
  194. /**
  195. * @return array
  196. */
  197. public function findLanguageFromLocaleData(): array {
  198. return [
  199. 'en_US' => ['en_US', 'en'],
  200. 'en_UK' => ['en_UK', 'en'],
  201. 'de_DE' => ['de_DE', 'de_DE'],
  202. 'de_AT' => ['de_AT', 'de'],
  203. 'es_EC' => ['es_EC', 'es_EC'],
  204. 'fi_FI' => ['fi_FI', 'fi'],
  205. 'zh_CN' => ['zh_CN', 'zh_CN'],
  206. ];
  207. }
  208. }