TranslatorTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. public function testTransNonExistentWithFallback()
  52. {
  53. $translator = new Translator('fr', new MessageSelector());
  54. $translator->setFallbackLocale('en');
  55. $translator->addLoader('array', new ArrayLoader());
  56. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  57. }
  58. /**
  59. * @expectedException RuntimeException
  60. */
  61. public function testWhenAResourceHasNoRegisteredLoader()
  62. {
  63. $translator = new Translator('en', new MessageSelector());
  64. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  65. $translator->trans('foo');
  66. }
  67. /**
  68. * @dataProvider getTransTests
  69. */
  70. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  71. {
  72. $translator = new Translator('en', new MessageSelector());
  73. $translator->addLoader('array', new ArrayLoader());
  74. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  75. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  76. }
  77. /**
  78. * @dataProvider getFlattenedTransTests
  79. */
  80. public function testFlattenedTrans($expected, $messages, $id)
  81. {
  82. $translator = new Translator('en', new MessageSelector());
  83. $translator->addLoader('array', new ArrayLoader());
  84. $translator->addResource('array', $messages, 'fr', '');
  85. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  86. }
  87. /**
  88. * @dataProvider getTransChoiceTests
  89. */
  90. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  91. {
  92. $translator = new Translator('en', new MessageSelector());
  93. $translator->addLoader('array', new ArrayLoader());
  94. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  95. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  96. }
  97. public function getTransTests()
  98. {
  99. return array(
  100. array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''),
  101. array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  102. array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''),
  103. );
  104. }
  105. public function getFlattenedTransTests()
  106. {
  107. $messages = array(
  108. 'symfony2' => array(
  109. 'is' => array(
  110. 'great' => 'Symfony2 est super!'
  111. )
  112. ),
  113. 'foo' => array(
  114. 'bar' => array(
  115. 'baz' => 'Foo Bar Baz'
  116. ),
  117. 'baz' => 'Foo Baz',
  118. ),
  119. );
  120. return array(
  121. array('Symfony2 est super!', $messages, 'symfony2.is.great'),
  122. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  123. array('Foo Baz', $messages, 'foo.baz'),
  124. );
  125. }
  126. public function getTransChoiceTests()
  127. {
  128. return array(
  129. 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', ''),
  130. 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', ''),
  131. 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', ''),
  132. 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', ''),
  133. 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', ''),
  134. 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', ''),
  135. 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', ''),
  136. 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', ''),
  137. 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', ''),
  138. 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', ''),
  139. 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', ''),
  140. 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', ''),
  141. 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', ''),
  142. );
  143. }
  144. public function testTransChoiceFallback()
  145. {
  146. $translator = new Translator('ru', new MessageSelector());
  147. $translator->setFallbackLocale('en');
  148. $translator->addLoader('array', new ArrayLoader());
  149. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
  150. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  151. }
  152. }
  153. class String
  154. {
  155. protected $str;
  156. public function __construct($str)
  157. {
  158. $this->str = $str;
  159. }
  160. public function __toString()
  161. {
  162. return $this->str;
  163. }
  164. }