I18nTest.php 8.1 KB

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