TranslatorTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Translation;
  11. use Symfony\Component\Translation\Translator;
  12. use Symfony\Component\Translation\MessageSelector;
  13. use Symfony\Component\Translation\Loader\ArrayLoader;
  14. class TranslatorTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testSetGetLocale()
  17. {
  18. $translator = new Translator('en', new MessageSelector());
  19. $this->assertEquals('en', $translator->getLocale());
  20. $translator->setLocale('fr');
  21. $this->assertEquals('fr', $translator->getLocale());
  22. }
  23. public function testSetFallbackLocale()
  24. {
  25. $translator = new Translator('en', new MessageSelector());
  26. $translator->addLoader('array', new ArrayLoader());
  27. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  28. $translator->addResource('array', array('bar' => 'foobar'), 'fr');
  29. // force catalogue loading
  30. $translator->trans('bar');
  31. $translator->setFallbackLocale('fr');
  32. $this->assertEquals('foobar', $translator->trans('bar'));
  33. }
  34. public function testTransWithFallbackLocale()
  35. {
  36. $translator = new Translator('fr_FR', new MessageSelector());
  37. $translator->addLoader('array', new ArrayLoader());
  38. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  39. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  40. $translator->setFallbackLocale('en');
  41. $this->assertEquals('foobar', $translator->trans('bar'));
  42. }
  43. public function testTransWithFallbackLocaleBis()
  44. {
  45. $translator = new Translator('en_US', new MessageSelector());
  46. $translator->addLoader('array', new ArrayLoader());
  47. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  48. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  49. $this->assertEquals('foobar', $translator->trans('bar'));
  50. }
  51. /**
  52. * @expectedException RuntimeException
  53. */
  54. public function testWhenAResourceHasNoRegisteredLoader()
  55. {
  56. $translator = new Translator('en', new MessageSelector());
  57. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  58. $translator->trans('foo');
  59. }
  60. /**
  61. * @dataProvider getTransTests
  62. */
  63. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  64. {
  65. $translator = new Translator('en', new MessageSelector());
  66. $translator->addLoader('array', new ArrayLoader());
  67. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  68. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  69. }
  70. /**
  71. * @dataProvider getFlattenedTransTests
  72. */
  73. public function testFlattenedTrans($expected, $messages, $id)
  74. {
  75. $translator = new Translator('en', new MessageSelector());
  76. $translator->addLoader('array', new ArrayLoader());
  77. $translator->addResource('array', $messages, 'fr', '');
  78. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  79. }
  80. /**
  81. * @dataProvider getTransChoiceTests
  82. */
  83. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  84. {
  85. $translator = new Translator('en', new MessageSelector());
  86. $translator->addLoader('array', new ArrayLoader());
  87. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  88. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  89. }
  90. public function getTransTests()
  91. {
  92. return array(
  93. array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''),
  94. array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  95. array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''),
  96. );
  97. }
  98. public function getFlattenedTransTests()
  99. {
  100. $messages = array(
  101. 'symfony2' => array(
  102. 'is' => array(
  103. 'great' => 'Symfony2 est super!'
  104. )
  105. ),
  106. 'foo' => array(
  107. 'bar' => array(
  108. 'baz' => 'Foo Bar Baz'
  109. ),
  110. 'baz' => 'Foo Baz',
  111. ),
  112. );
  113. return array(
  114. array('Symfony2 est super!', $messages, 'symfony2.is.great'),
  115. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  116. array('Foo Baz', $messages, 'foo.baz'),
  117. );
  118. }
  119. public function getTransChoiceTests()
  120. {
  121. return array(
  122. array('Il y a 0 pomme', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''),
  123. array('Il y a 1 pomme', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''),
  124. array('Il y a 10 pommes', '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''),
  125. array('Il y a 0 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''),
  126. array('Il y a 1 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''),
  127. array('Il y a 10 pommes', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''),
  128. array('Il y a 0 pomme', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''),
  129. array('Il y a 1 pomme', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''),
  130. array('Il y a 10 pommes', 'one: There is one apple|more: There is %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''),
  131. array('Il n\'y a aucune pomme', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''),
  132. array('Il y a 1 pomme', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, array('%count%' => 1), 'fr', ''),
  133. array('Il y a 10 pommes', '{0} There is no apple|one: There is one apple|more: There is %count% apples', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, array('%count%' => 10), 'fr', ''),
  134. array('Il y a 0 pomme', new String('{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples'), '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, array('%count%' => 0), 'fr', ''),
  135. );
  136. }
  137. }
  138. class String
  139. {
  140. protected $str;
  141. public function __construct($str)
  142. {
  143. $this->str = $str;
  144. }
  145. public function __toString()
  146. {
  147. return $this->str;
  148. }
  149. }