L10nTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 dataPlaceholders(): array {
  76. return [
  77. ['Ordered placeholders one %s two %s', 'Placeholder one 1 two 2'],
  78. ['Reordered placeholders one %s two %s', 'Placeholder two 2 one 1'],
  79. ['Reordered placeholders one %1$s two %2$s', 'Placeholder two 2 one 1'],
  80. ];
  81. }
  82. /**
  83. * @dataProvider dataPlaceholders
  84. *
  85. * @param $string
  86. * @param $expected
  87. */
  88. public function testPlaceholders($string, $expected): void {
  89. $transFile = \OC::$SERVERROOT.'/tests/data/l10n/de.json';
  90. $l = new L10N($this->getFactory(), 'test', 'de', 'de_AT', [$transFile]);
  91. $this->assertEquals($expected, $l->t($string, ['1', '2']));
  92. }
  93. public function localizationData() {
  94. return [
  95. // timestamp as string
  96. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', '1234567890'],
  97. ['13. Februar 2009 um 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', '1234567890'],
  98. ['February 13, 2009', 'en', 'en_US', 'date', '1234567890'],
  99. ['13. Februar 2009', 'de', 'de_DE', 'date', '1234567890'],
  100. ['11:31:30 PM GMT+0', 'en', 'en_US', 'time', '1234567890'],
  101. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', '1234567890'],
  102. // timestamp as int
  103. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', 1234567890],
  104. ['13. Februar 2009 um 23:31:30 GMT+0', '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 PM GMT+0', 'en', 'en_US', 'time', 1234567890],
  108. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', 1234567890],
  109. // DateTime object
  110. ['February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'en_US', 'datetime', new DateTime('@1234567890')],
  111. ['13. Februar 2009 um 23:31:30 GMT+0', 'de', 'de_DE', 'datetime', new DateTime('@1234567890')],
  112. ['February 13, 2009', 'en', 'en_US', 'date', new DateTime('@1234567890')],
  113. ['13. Februar 2009', 'de', 'de_DE', 'date', new DateTime('@1234567890')],
  114. ['11:31:30 PM GMT+0', 'en', 'en_US', 'time', new DateTime('@1234567890')],
  115. ['23:31:30 GMT+0', 'de', 'de_DE', 'time', new DateTime('@1234567890')],
  116. // en_GB
  117. ['13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  118. ['13 February 2009', 'en_GB', 'en_GB', 'date', new DateTime('@1234567890')],
  119. ['23:31:30 GMT+0', 'en_GB', 'en_GB', 'time', new DateTime('@1234567890')],
  120. ['13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'en_GB', 'datetime', new DateTime('@1234567890')],
  121. ['13 February 2009', 'en-GB', 'en_GB', 'date', new DateTime('@1234567890')],
  122. ['23:31:30 GMT+0', 'en-GB', 'en_GB', 'time', new DateTime('@1234567890')],
  123. ];
  124. }
  125. /**
  126. * @dataProvider localizationData
  127. */
  128. public function testNumericStringLocalization($expectedDate, $lang, $locale, $type, $value) {
  129. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  130. $this->assertSame($expectedDate, $l->l($type, $value));
  131. }
  132. public function firstDayData() {
  133. return [
  134. [1, 'de', 'de_DE'],
  135. [0, 'en', 'en_US'],
  136. ];
  137. }
  138. /**
  139. * @dataProvider firstDayData
  140. * @param $expected
  141. * @param $lang
  142. * @param $locale
  143. */
  144. public function testFirstWeekDay($expected, $lang, $locale) {
  145. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  146. $this->assertSame($expected, $l->l('firstday', 'firstday'));
  147. }
  148. public function jsDateData() {
  149. return [
  150. ['dd.MM.yy', 'de', 'de_DE'],
  151. ['M/d/yy', 'en', 'en_US'],
  152. ];
  153. }
  154. /**
  155. * @dataProvider jsDateData
  156. * @param $expected
  157. * @param $lang
  158. * @param $locale
  159. */
  160. public function testJSDate($expected, $lang, $locale) {
  161. $l = new L10N($this->getFactory(), 'test', $lang, $locale, []);
  162. $this->assertSame($expected, $l->l('jsdate', 'jsdate'));
  163. }
  164. public function testFactoryGetLanguageCode() {
  165. $l = $this->getFactory()->get('lib', 'de');
  166. $this->assertEquals('de', $l->getLanguageCode());
  167. }
  168. public function testServiceGetLanguageCode() {
  169. $l = \OC::$server->getL10N('lib', 'de');
  170. $this->assertEquals('de', $l->getLanguageCode());
  171. }
  172. public function testWeekdayName() {
  173. $l = \OC::$server->getL10N('lib', 'de');
  174. $this->assertEquals('Mo.', $l->l('weekdayName', new \DateTime('2017-11-6'), ['width' => 'abbreviated']));
  175. }
  176. /**
  177. * @dataProvider findLanguageFromLocaleData
  178. * @param $locale
  179. * @param $language
  180. */
  181. public function testFindLanguageFromLocale($locale, $language) {
  182. $this->assertEquals(
  183. $language,
  184. \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale)
  185. );
  186. }
  187. /**
  188. * @return array
  189. */
  190. public function findLanguageFromLocaleData(): array {
  191. return [
  192. 'en_US' => ['en_US', 'en'],
  193. 'en_UK' => ['en_UK', 'en'],
  194. 'de_DE' => ['de_DE', 'de_DE'],
  195. 'de_AT' => ['de_AT', 'de'],
  196. 'es_EC' => ['es_EC', 'es_EC'],
  197. 'fi_FI' => ['fi_FI', 'fi'],
  198. 'zh_CN' => ['zh_CN', 'zh_CN'],
  199. ];
  200. }
  201. }