TranslationExtensionTest.php 5.1 KB

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