i18n.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. class i18nTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_translations = array();
  5. public function setUp()
  6. {
  7. /* Setup Routine */
  8. $this->_translations = json_decode(
  9. file_get_contents(PATH . 'i18n' . DIRECTORY_SEPARATOR . 'de.json'),
  10. true
  11. );
  12. }
  13. public function tearDown()
  14. {
  15. /* Tear Down Routine */
  16. }
  17. public function testTranslationFallback()
  18. {
  19. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  20. $messageId = 'It does not matter if the message ID exists';
  21. i18n::loadTranslations();
  22. $this->assertEquals($messageId, i18n::_($messageId), 'fallback to en');
  23. }
  24. public function testBrowserLanguageDeDetection()
  25. {
  26. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  27. i18n::loadTranslations();
  28. $this->assertEquals($this->_translations['en'], i18n::_('en'), 'browser language de');
  29. $this->assertEquals('0 Stunden', i18n::_('%d hours', 0), '0 hours in german');
  30. $this->assertEquals('1 Stunde', i18n::_('%d hours', 1), '1 hour in german');
  31. $this->assertEquals('2 Stunden', i18n::_('%d hours', 2), '2 hours in french');
  32. }
  33. public function testBrowserLanguageFrDetection()
  34. {
  35. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  36. i18n::loadTranslations();
  37. $this->assertEquals('fr', i18n::_('en'), 'browser language fr');
  38. $this->assertEquals('0 heure', i18n::_('%d hours', 0), '0 hours in french');
  39. $this->assertEquals('1 heure', i18n::_('%d hours', 1), '1 hour in french');
  40. $this->assertEquals('2 heures', i18n::_('%d hours', 2), '2 hours in french');
  41. }
  42. public function testBrowserLanguagePlDetection()
  43. {
  44. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  45. i18n::loadTranslations();
  46. $this->assertEquals('pl', i18n::_('en'), 'browser language pl');
  47. $this->assertEquals('2 godzina', i18n::_('%d hours', 2), 'hours in polish');
  48. }
  49. public function testBrowserLanguageAnyDetection()
  50. {
  51. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  52. i18n::loadTranslations();
  53. $this->assertTrue(strlen(i18n::_('en')) == 2, 'browser language any');
  54. }
  55. public function testVariableInjection()
  56. {
  57. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  58. i18n::loadTranslations();
  59. $this->assertEquals('some string + 1', i18n::_('some %s + %d', 'string', 1), 'browser language de');
  60. }
  61. }