TranslatorTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  44. * @expectedException RuntimeException
  45. */
  46. public function testWhenAResourceHasNoRegisteredLoader()
  47. {
  48. $translator = new Translator('en', new MessageSelector());
  49. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  50. $translator->trans('foo');
  51. }
  52. /**
  53. * @dataProvider getTransTests
  54. */
  55. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  56. {
  57. $translator = new Translator('en', new MessageSelector());
  58. $translator->addLoader('array', new ArrayLoader());
  59. $translator->addResource('array', array($id => $translation), $locale, $domain);
  60. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  61. }
  62. /**
  63. * @dataProvider getFlattenedTransTests
  64. */
  65. public function testFlattenedTrans($expected, $messages, $id)
  66. {
  67. $translator = new Translator('en', new MessageSelector());
  68. $translator->addLoader('array', new ArrayLoader());
  69. $translator->addResource('array', $messages, 'fr', '');
  70. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  71. }
  72. /**
  73. * @dataProvider getTransChoiceTests
  74. */
  75. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  76. {
  77. $translator = new Translator('en', new MessageSelector());
  78. $translator->addLoader('array', new ArrayLoader());
  79. $translator->addResource('array', array($id => $translation), $locale, $domain);
  80. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  81. }
  82. public function getTransTests()
  83. {
  84. return array(
  85. array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''),
  86. array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  87. );
  88. }
  89. public function getFlattenedTransTests()
  90. {
  91. $messages = array(
  92. 'symfony2' => array(
  93. 'is' => array(
  94. 'great' => 'Symfony2 est super!'
  95. )
  96. ),
  97. 'foo' => array(
  98. 'bar' => array(
  99. 'baz' => 'Foo Bar Baz'
  100. ),
  101. 'baz' => 'Foo Baz',
  102. ),
  103. );
  104. return array(
  105. array('Symfony2 est super!', $messages, 'symfony2.is.great'),
  106. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  107. array('Foo Baz', $messages, 'foo.baz'),
  108. );
  109. }
  110. public function getTransChoiceTests()
  111. {
  112. return array(
  113. 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', ''),
  114. 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', ''),
  115. 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', ''),
  116. 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', ''),
  117. 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', ''),
  118. 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', ''),
  119. 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', ''),
  120. 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', ''),
  121. 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', ''),
  122. 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', ''),
  123. 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', ''),
  124. 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', ''),
  125. );
  126. }
  127. }