l10n.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_L10n extends \Test\TestCase {
  9. public function testGermanPluralTranslations() {
  10. $l = new OC_L10N('test');
  11. $transFile = OC::$SERVERROOT.'/tests/data/l10n/de.json';
  12. $l->load($transFile);
  13. $this->assertEquals('1 Datei', (string)$l->n('%n file', '%n files', 1));
  14. $this->assertEquals('2 Dateien', (string)$l->n('%n file', '%n files', 2));
  15. }
  16. public function testRussianPluralTranslations() {
  17. $l = new OC_L10N('test');
  18. $transFile = OC::$SERVERROOT.'/tests/data/l10n/ru.json';
  19. $l->load($transFile);
  20. $this->assertEquals('1 файл', (string)$l->n('%n file', '%n files', 1));
  21. $this->assertEquals('2 файла', (string)$l->n('%n file', '%n files', 2));
  22. $this->assertEquals('6 файлов', (string)$l->n('%n file', '%n files', 6));
  23. $this->assertEquals('21 файл', (string)$l->n('%n file', '%n files', 21));
  24. $this->assertEquals('22 файла', (string)$l->n('%n file', '%n files', 22));
  25. $this->assertEquals('26 файлов', (string)$l->n('%n file', '%n files', 26));
  26. /*
  27. 1 file 1 файл 1 папка
  28. 2-4 files 2-4 файла 2-4 папки
  29. 5-20 files 5-20 файлов 5-20 папок
  30. 21 files 21 файл 21 папка
  31. 22-24 files 22-24 файла 22-24 папки
  32. 25-30 files 25-30 файлов 25-30 папок
  33. etc
  34. 100 files 100 файлов, 100 папок
  35. 1000 files 1000 файлов 1000 папок
  36. */
  37. }
  38. public function testCzechPluralTranslations() {
  39. $l = new OC_L10N('test');
  40. $transFile = OC::$SERVERROOT.'/tests/data/l10n/cs.json';
  41. $l->load($transFile);
  42. $this->assertEquals('1 okno', (string)$l->n('%n window', '%n windows', 1));
  43. $this->assertEquals('2 okna', (string)$l->n('%n window', '%n windows', 2));
  44. $this->assertEquals('5 oken', (string)$l->n('%n window', '%n windows', 5));
  45. }
  46. public function localizationDataProvider() {
  47. return array(
  48. // timestamp as string
  49. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', '1234567890'),
  50. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', '1234567890'),
  51. array('February 13, 2009', 'en', 'date', '1234567890'),
  52. array('13. Februar 2009', 'de', 'date', '1234567890'),
  53. array('11:31:30 PM GMT+0', 'en', 'time', '1234567890'),
  54. array('23:31:30 GMT+0', 'de', 'time', '1234567890'),
  55. // timestamp as int
  56. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', 1234567890),
  57. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', 1234567890),
  58. array('February 13, 2009', 'en', 'date', 1234567890),
  59. array('13. Februar 2009', 'de', 'date', 1234567890),
  60. array('11:31:30 PM GMT+0', 'en', 'time', 1234567890),
  61. array('23:31:30 GMT+0', 'de', 'time', 1234567890),
  62. // DateTime object
  63. array('February 13, 2009 at 11:31:30 PM GMT+0', 'en', 'datetime', new DateTime('@1234567890')),
  64. array('13. Februar 2009 um 23:31:30 GMT+0', 'de', 'datetime', new DateTime('@1234567890')),
  65. array('February 13, 2009', 'en', 'date', new DateTime('@1234567890')),
  66. array('13. Februar 2009', 'de', 'date', new DateTime('@1234567890')),
  67. array('11:31:30 PM GMT+0', 'en', 'time', new DateTime('@1234567890')),
  68. array('23:31:30 GMT+0', 'de', 'time', new DateTime('@1234567890')),
  69. // en_GB
  70. array('13 February 2009 at 23:31:30 GMT+0', 'en_GB', 'datetime', new DateTime('@1234567890')),
  71. array('13 February 2009', 'en_GB', 'date', new DateTime('@1234567890')),
  72. array('23:31:30 GMT+0', 'en_GB', 'time', new DateTime('@1234567890')),
  73. array('13 February 2009 at 23:31:30 GMT+0', 'en-GB', 'datetime', new DateTime('@1234567890')),
  74. array('13 February 2009', 'en-GB', 'date', new DateTime('@1234567890')),
  75. array('23:31:30 GMT+0', 'en-GB', 'time', new DateTime('@1234567890')),
  76. );
  77. }
  78. /**
  79. * @dataProvider localizationDataProvider
  80. */
  81. public function testNumericStringLocalization($expectedDate, $lang, $type, $value) {
  82. $l = new OC_L10N('test');
  83. $l->forceLanguage($lang);
  84. $this->assertSame($expectedDate, $l->l($type, $value));
  85. }
  86. public function firstDayDataProvider() {
  87. return array(
  88. array(1, 'de'),
  89. array(0, 'en'),
  90. );
  91. }
  92. /**
  93. * @dataProvider firstDayDataProvider
  94. * @param $expected
  95. * @param $lang
  96. */
  97. public function testFirstWeekDay($expected, $lang) {
  98. $l = new OC_L10N('test');
  99. $l->forceLanguage($lang);
  100. $this->assertSame($expected, $l->l('firstday', 'firstday'));
  101. }
  102. /**
  103. * @dataProvider findLanguageData
  104. */
  105. public function testFindLanguage($default, $preference, $expected) {
  106. OC_User::setUserId(null);
  107. if (is_null($default)) {
  108. OC_Config::deleteKey('default_language');
  109. } else {
  110. OC_Config::setValue('default_language', $default);
  111. }
  112. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = $preference;
  113. $reflection = new \ReflectionClass('OC_L10N');
  114. $prop = $reflection->getProperty('language');
  115. $prop->setAccessible(1);
  116. $prop->setValue('');
  117. $prop->setAccessible(0);
  118. $this->assertSame($expected, OC_L10N::findLanguage());
  119. }
  120. public function findLanguageData() {
  121. return array(
  122. // Exact match
  123. array(null, 'de-DE,en;q=0.5', 'de_DE'),
  124. array(null, 'de-DE,en-US;q=0.8,en;q=0.6', 'de_DE'),
  125. // Best match
  126. array(null, 'de-US,en;q=0.5', 'de'),
  127. array(null, 'de-US,en-US;q=0.8,en;q=0.6', 'de'),
  128. // The default_language config setting overrides browser preferences.
  129. array('es_AR', 'de-DE,en;q=0.5', 'es_AR'),
  130. array('es_AR', 'de-DE,en-US;q=0.8,en;q=0.6', 'es_AR'),
  131. // Worst case default to english
  132. array(null, '', 'en'),
  133. array(null, null, 'en'),
  134. );
  135. }
  136. public function testGetLanguageCode() {
  137. $l = OC_L10N::get('lib', 'de');
  138. $this->assertEquals('de', $l->getLanguageCode());
  139. }
  140. public function testFactoryGetLanguageCode() {
  141. $factory = new \OC\L10N\Factory();
  142. $l = $factory->get('lib', 'de');
  143. $this->assertEquals('de', $l->getLanguageCode());
  144. }
  145. public function testServiceGetLanguageCode() {
  146. $l = \OC::$server->getL10N('lib', 'de');
  147. $this->assertEquals('de', $l->getLanguageCode());
  148. }
  149. }