I18nTest.php 9.4 KB

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