TransTest.php 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Bridge\Twig;
  11. use Symfony\Bridge\Twig\Extension\TransExtension;
  12. use Symfony\Component\Translation\Translator;
  13. use Symfony\Component\Translation\MessageSelector;
  14. class TransTest extends TestCase
  15. {
  16. protected function setUp()
  17. {
  18. if (!class_exists('Twig_Environment')) {
  19. $this->markTestSkipped('Twig is not available.');
  20. }
  21. }
  22. /**
  23. * @dataProvider getTransTests
  24. */
  25. public function testTrans($template, $expected, array $variables = array())
  26. {
  27. if ($expected != $this->getTemplate($template)->render($variables)) {
  28. print $template."\n";
  29. $loader = new \Twig_Loader_Array(array('index' => $template));
  30. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  31. $twig->addExtension(new TransExtension(new Translator('en', new MessageSelector())));
  32. echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
  33. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  34. }
  35. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  36. }
  37. public function getTransTests()
  38. {
  39. return array(
  40. // trans tag
  41. array('{% trans "Hello" %}', 'Hello'),
  42. array('{% trans "Hello %name%" %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  43. array('{% trans name %}', 'Symfony2', array('name' => 'Symfony2')),
  44. array('{% trans hello with { \'%name%\': \'Symfony2\' } %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  45. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans hello with vars %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  46. array('{% trans %}Hello{% endtrans %}', 'Hello'),
  47. array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
  48. array('{% trans "Hello" from elsewhere %}', 'Hello'),
  49. array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  50. array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  51. array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  52. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  53. // transchoice
  54. array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  55. 'There is no apples', array('count' => 0)),
  56. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  57. 'There is 5 apples', array('count' => 5)),
  58. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  59. 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
  60. array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  61. 'There is 5 apples (Symfony2)', array('count' => 5)),
  62. // trans filter
  63. array('{{ "Hello"|trans }}', 'Hello'),
  64. array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  65. array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  66. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  67. );
  68. }
  69. protected function getTemplate($template)
  70. {
  71. $loader = new \Twig_Loader_Array(array('index' => $template));
  72. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  73. $twig->addExtension(new TransExtension(new Translator('en', new MessageSelector())));
  74. return $twig->loadTemplate('index');
  75. }
  76. }