I18nTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. use PrivateBin\I18n;
  3. class I18nTest extends PHPUnit_Framework_TestCase
  4. {
  5. private $_translations = array();
  6. public function setUp()
  7. {
  8. /* Setup Routine */
  9. $this->_translations = json_decode(
  10. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  11. true
  12. );
  13. }
  14. public function tearDown()
  15. {
  16. /* Tear Down Routine */
  17. }
  18. public function testTranslationFallback()
  19. {
  20. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  21. $messageId = 'It does not matter if the message ID exists';
  22. I18n::loadTranslations();
  23. $this->assertEquals($messageId, I18n::_($messageId), 'fallback to en');
  24. I18n::getLanguageLabels();
  25. }
  26. public function testCookieLanguageDeDetection()
  27. {
  28. $_COOKIE['lang'] = 'de';
  29. I18n::loadTranslations();
  30. $this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
  31. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  32. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  33. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  34. }
  35. public function testBrowserLanguageDeDetection()
  36. {
  37. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,fr;q=0.0';
  38. I18n::loadTranslations();
  39. $this->assertEquals($this->_translations['en'], I18n::_('en'), 'browser language de');
  40. $this->assertEquals('0 Stunden', I18n::_('%d hours', 0), '0 hours in German');
  41. $this->assertEquals('1 Stunde', I18n::_('%d hours', 1), '1 hour in German');
  42. $this->assertEquals('2 Stunden', I18n::_('%d hours', 2), '2 hours in German');
  43. }
  44. public function testBrowserLanguageFrDetection()
  45. {
  46. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2,de;q=0.0';
  47. I18n::loadTranslations();
  48. $this->assertEquals('fr', I18n::_('en'), 'browser language fr');
  49. $this->assertEquals('0 heure', I18n::_('%d hours', 0), '0 hours in French');
  50. $this->assertEquals('1 heure', I18n::_('%d hours', 1), '1 hour in French');
  51. $this->assertEquals('2 heures', I18n::_('%d hours', 2), '2 hours in French');
  52. }
  53. public function testBrowserLanguageNoDetection()
  54. {
  55. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'no;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  56. I18n::loadTranslations();
  57. $this->assertEquals('no', I18n::_('en'), 'browser language no');
  58. $this->assertEquals('0 timer', I18n::_('%d hours', 0), '0 hours in Norwegian');
  59. $this->assertEquals('1 time', I18n::_('%d hours', 1), '1 hour in Norwegian');
  60. $this->assertEquals('2 timer', I18n::_('%d hours', 2), '2 hours in Norwegian');
  61. }
  62. public function testBrowserLanguageOcDetection()
  63. {
  64. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  65. I18n::loadTranslations();
  66. $this->assertEquals('oc', I18n::_('en'), 'browser language oc');
  67. $this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
  68. $this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
  69. $this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
  70. }
  71. public function testBrowserLanguageZhDetection()
  72. {
  73. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  74. I18n::loadTranslations();
  75. $this->assertEquals('zh', I18n::_('en'), 'browser language zh');
  76. $this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
  77. $this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
  78. $this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
  79. }
  80. public function testBrowserLanguagePlDetection()
  81. {
  82. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  83. I18n::loadTranslations();
  84. $this->assertEquals('pl', I18n::_('en'), 'browser language pl');
  85. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
  86. $this->assertEquals('2 godzina', I18n::_('%d hours', 2), '2 hours in Polish');
  87. $this->assertEquals('12 godzinę', I18n::_('%d hours', 12), '12 hours in Polish');
  88. $this->assertEquals('22 godzina', I18n::_('%d hours', 22), '22 hours in Polish');
  89. $this->assertEquals('1 minut', I18n::_('%d minutes', 1), '1 minute in Polish');
  90. $this->assertEquals('3 minut', I18n::_('%d minutes', 3), '3 minutes in Polish');
  91. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
  92. $this->assertEquals('23 minut', I18n::_('%d minutes', 23), '23 minutes in Polish');
  93. }
  94. public function testBrowserLanguageRuDetection()
  95. {
  96. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  97. I18n::loadTranslations();
  98. $this->assertEquals('ru', I18n::_('en'), 'browser language ru');
  99. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
  100. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
  101. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
  102. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
  103. }
  104. public function testBrowserLanguageSlDetection()
  105. {
  106. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  107. I18n::loadTranslations();
  108. $this->assertEquals('sl', I18n::_('en'), 'browser language sl');
  109. $this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in Slowene');
  110. $this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in Slowene');
  111. $this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in Slowene');
  112. $this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in Slowene');
  113. $this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in Slowene');
  114. $this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in Slowene');
  115. $this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in Slowene');
  116. $this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in Slowene');
  117. }
  118. public function testBrowserLanguageCsDetection()
  119. {
  120. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'cs;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  121. I18n::loadTranslations();
  122. $this->assertEquals('cs', I18n::_('en'), 'browser language cs');
  123. $this->assertEquals('1 hodin', I18n::_('%d hours', 1), '1 hour in Czech');
  124. $this->assertEquals('2 hodiny', I18n::_('%d hours', 2), '2 hours in Czech');
  125. $this->assertEquals('5 minut', I18n::_('%d minutes', 5), '5 minutes in Czech');
  126. $this->assertEquals('14 minut', I18n::_('%d minutes', 14), '14 minutes in Czech');
  127. }
  128. public function testBrowserLanguageAnyDetection()
  129. {
  130. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  131. I18n::loadTranslations();
  132. $this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
  133. }
  134. public function testVariableInjection()
  135. {
  136. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  137. I18n::loadTranslations();
  138. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  139. }
  140. public function testMessageIdsExistInAllLanguages()
  141. {
  142. $messageIds = array();
  143. $languages = array();
  144. $dir = dir(PATH . 'i18n');
  145. while (false !== ($file = $dir->read())) {
  146. if (strlen($file) === 7) {
  147. $language = substr($file, 0, 2);
  148. $languageMessageIds = array_keys(
  149. json_decode(
  150. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . $file),
  151. true
  152. )
  153. );
  154. $messageIds = array_unique(array_merge($messageIds, $languageMessageIds));
  155. $languages[$language] = $languageMessageIds;
  156. }
  157. }
  158. foreach ($messageIds as $messageId) {
  159. foreach (array_keys($languages) as $language) {
  160. // most languages don't translate the data size units, ignore those
  161. if ($messageId !== 'B' && strlen($messageId) !== 3 && strpos($messageId, 'B', 2) !== 2) {
  162. $this->assertContains(
  163. $messageId,
  164. $languages[$language],
  165. "message ID '$messageId' exists in translation file $language.json"
  166. );
  167. }
  168. }
  169. }
  170. }
  171. }