TranslatorTest.php 6.7 KB

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