I18nTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 testBrowserLanguageOcDetection()
  53. {
  54. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'oc;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  55. I18n::loadTranslations();
  56. $this->assertEquals('oc', I18n::_('en'), 'browser language oc');
  57. $this->assertEquals('0 ora', I18n::_('%d hours', 0), '0 hours in Occitan');
  58. $this->assertEquals('1 ora', I18n::_('%d hours', 1), '1 hour in Occitan');
  59. $this->assertEquals('2 oras', I18n::_('%d hours', 2), '2 hours in Occitan');
  60. }
  61. public function testBrowserLanguageZhDetection()
  62. {
  63. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'zh;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  64. I18n::loadTranslations();
  65. $this->assertEquals('zh', I18n::_('en'), 'browser language zh');
  66. $this->assertEquals('0 小时', I18n::_('%d hours', 0), '0 hours in Chinese');
  67. $this->assertEquals('1 小时', I18n::_('%d hours', 1), '1 hour in Chinese');
  68. $this->assertEquals('2 小时', I18n::_('%d hours', 2), '2 hours in Chinese');
  69. }
  70. public function testBrowserLanguagePlDetection()
  71. {
  72. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  73. I18n::loadTranslations();
  74. $this->assertEquals('pl', I18n::_('en'), 'browser language pl');
  75. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in Polish');
  76. $this->assertEquals('2 godzina', I18n::_('%d hours', 2), '2 hours in Polish');
  77. $this->assertEquals('12 godzinę', I18n::_('%d hours', 12), '12 hours in Polish');
  78. $this->assertEquals('22 godzina', I18n::_('%d hours', 22), '22 hours in Polish');
  79. $this->assertEquals('1 minut', I18n::_('%d minutes', 1), '1 minute in Polish');
  80. $this->assertEquals('3 minut', I18n::_('%d minutes', 3), '3 minutes in Polish');
  81. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in Polish');
  82. $this->assertEquals('23 minut', I18n::_('%d minutes', 23), '23 minutes in Polish');
  83. }
  84. public function testBrowserLanguageRuDetection()
  85. {
  86. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  87. I18n::loadTranslations();
  88. $this->assertEquals('ru', I18n::_('en'), 'browser language ru');
  89. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in Russian');
  90. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in Russian');
  91. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in Russian');
  92. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in Russian');
  93. }
  94. public function testBrowserLanguageSlDetection()
  95. {
  96. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  97. I18n::loadTranslations();
  98. $this->assertEquals('sl', I18n::_('en'), 'browser language sl');
  99. $this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in Slowene');
  100. $this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in Slowene');
  101. $this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in Slowene');
  102. $this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in Slowene');
  103. $this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in Slowene');
  104. $this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in Slowene');
  105. $this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in Slowene');
  106. $this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in Slowene');
  107. }
  108. public function testBrowserLanguageAnyDetection()
  109. {
  110. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  111. I18n::loadTranslations();
  112. $this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
  113. }
  114. public function testVariableInjection()
  115. {
  116. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  117. I18n::loadTranslations();
  118. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  119. }
  120. }