i18n.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 testCookieLanguageDeDetection()
  25. {
  26. $_COOKIE['lang'] = 'de';
  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 testBrowserLanguageDeDetection()
  34. {
  35. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  36. i18n::loadTranslations();
  37. $this->assertEquals($this->_translations['en'], i18n::_('en'), 'browser language de');
  38. $this->assertEquals('0 Stunden', i18n::_('%d hours', 0), '0 hours in german');
  39. $this->assertEquals('1 Stunde', i18n::_('%d hours', 1), '1 hour in german');
  40. $this->assertEquals('2 Stunden', i18n::_('%d hours', 2), '2 hours in french');
  41. }
  42. public function testBrowserLanguageFrDetection()
  43. {
  44. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'fr-CH,fr;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  45. i18n::loadTranslations();
  46. $this->assertEquals('fr', i18n::_('en'), 'browser language fr');
  47. $this->assertEquals('0 heure', i18n::_('%d hours', 0), '0 hours in french');
  48. $this->assertEquals('1 heure', i18n::_('%d hours', 1), '1 hour in french');
  49. $this->assertEquals('2 heures', i18n::_('%d hours', 2), '2 hours in french');
  50. }
  51. public function testBrowserLanguagePlDetection()
  52. {
  53. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'pl;q=0.8,en-GB;q=0.6,en-US;q=0.4,en;q=0.2';
  54. i18n::loadTranslations();
  55. $this->assertEquals('pl', i18n::_('en'), 'browser language pl');
  56. $this->assertEquals('2 godzina', i18n::_('%d hours', 2), 'hours in polish');
  57. }
  58. public function testBrowserLanguageAnyDetection()
  59. {
  60. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = '*';
  61. i18n::loadTranslations();
  62. $this->assertTrue(strlen(i18n::_('en')) == 2, 'browser language any');
  63. }
  64. public function testVariableInjection()
  65. {
  66. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'foobar';
  67. i18n::loadTranslations();
  68. $this->assertEquals('some string + 1', i18n::_('some %s + %d', 'string', 1), 'browser language en');
  69. }
  70. }