TranslationExtensionTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  24. * @dataProvider getTransTests
  25. */
  26. public function testTrans($template, $expected, array $variables = array())
  27. {
  28. if ($expected != $this->getTemplate($template)->render($variables)) {
  29. print $template."\n";
  30. $loader = new \Twig_Loader_Array(array('index' => $template));
  31. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  32. $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  33. echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
  34. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  35. }
  36. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  37. }
  38. public function getTransTests()
  39. {
  40. return array(
  41. // trans tag
  42. array('{% trans %}Hello{% endtrans %}', 'Hello'),
  43. array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
  44. array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  45. array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  46. array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  47. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  48. // transchoice
  49. array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  50. 'There is no apples', array('count' => 0)),
  51. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  52. 'There is 5 apples', array('count' => 5)),
  53. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  54. 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
  55. array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  56. 'There is 5 apples (Symfony2)', array('count' => 5)),
  57. array('{% transchoice count for "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples" from "messages" %}',
  58. 'There is one apple', array('count' => 1)),
  59. array('{% set text = "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)" %}{% transchoice count for text with { \'%name%\': \'Symfony2\', \'%count%\': count } %}',
  60. 'There is 5 apples (Symfony2)', array('count' => 5)),
  61. // trans filter
  62. array('{{ "Hello"|trans }}', 'Hello'),
  63. array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  64. array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  65. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  66. // transchoice filter
  67. 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)),
  68. 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%)')),
  69. );
  70. }
  71. protected function getTemplate($template)
  72. {
  73. $loader = new \Twig_Loader_Array(array('index' => $template));
  74. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  75. $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  76. return $twig->loadTemplate('index');
  77. }
  78. }