I18nTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 french');
  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 french');
  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 testBrowserLanguagePlDetection()
  53. {
  54. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  55. I18n::loadTranslations();
  56. $this->assertEquals('pl', I18n::_('en'), 'browser language pl');
  57. $this->assertEquals('1 godzina', I18n::_('%d hours', 1), '1 hour in polish');
  58. $this->assertEquals('2 godzina', I18n::_('%d hours', 2), '2 hours in polish');
  59. $this->assertEquals('12 godzinę', I18n::_('%d hours', 12), '12 hours in polish');
  60. $this->assertEquals('22 godzina', I18n::_('%d hours', 22), '22 hours in polish');
  61. $this->assertEquals('1 minut', I18n::_('%d minutes', 1), '1 minute in polish');
  62. $this->assertEquals('3 minut', I18n::_('%d minutes', 3), '3 minutes in polish');
  63. $this->assertEquals('13 minut', I18n::_('%d minutes', 13), '13 minutes in polish');
  64. $this->assertEquals('23 minut', I18n::_('%d minutes', 23), '23 minutes in polish');
  65. }
  66. public function testBrowserLanguageRuDetection()
  67. {
  68. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ru;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  69. I18n::loadTranslations();
  70. $this->assertEquals('ru', I18n::_('en'), 'browser language ru');
  71. $this->assertEquals('1 минуту', I18n::_('%d minutes', 1), '1 minute in russian');
  72. $this->assertEquals('3 минуты', I18n::_('%d minutes', 3), '3 minutes in russian');
  73. $this->assertEquals('10 минут', I18n::_('%d minutes', 10), '10 minutes in russian');
  74. $this->assertEquals('21 минуту', I18n::_('%d minutes', 21), '21 minutes in russian');
  75. }
  76. public function testBrowserLanguageSlDetection()
  77. {
  78. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'sl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  79. I18n::loadTranslations();
  80. $this->assertEquals('sl', I18n::_('en'), 'browser language sl');
  81. $this->assertEquals('0 ura', I18n::_('%d hours', 0), '0 hours in slowene');
  82. $this->assertEquals('1 uri', I18n::_('%d hours', 1), '1 hour in slowene');
  83. $this->assertEquals('2 ure', I18n::_('%d hours', 2), '2 hours in slowene');
  84. $this->assertEquals('3 ur', I18n::_('%d hours', 3), '3 hours in slowene');
  85. $this->assertEquals('11 ura', I18n::_('%d hours', 11), '11 hours in slowene');
  86. $this->assertEquals('101 uri', I18n::_('%d hours', 101), '101 hours in slowene');
  87. $this->assertEquals('102 ure', I18n::_('%d hours', 102), '102 hours in slowene');
  88. $this->assertEquals('104 ur', I18n::_('%d hours', 104), '104 hours in slowene');
  89. }
  90. public function testBrowserLanguageAnyDetection()
  91. {
  92. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  93. I18n::loadTranslations();
  94. $this->assertTrue(strlen(I18n::_('en')) == 2, 'browser language any');
  95. }
  96. public function testVariableInjection()
  97. {
  98. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  99. I18n::loadTranslations();
  100. $this->assertEquals('some string + 1', I18n::_('some %s + %d', 'string', 1), 'browser language en');
  101. }
  102. }