TranslationExtensionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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" %}', 'Hello'),
  43. array('{% trans "Hello %name%" %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  44. array('{% trans name %}', 'Symfony2', array('name' => 'Symfony2')),
  45. array('{% trans hello with { \'%name%\': \'Symfony2\' } %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  46. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans hello with vars %}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  47. array('{% trans %}Hello{% endtrans %}', 'Hello'),
  48. array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
  49. array('{% trans "Hello" from elsewhere %}', 'Hello'),
  50. array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  51. array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  52. array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  53. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  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 for "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples" from "messages" %}',
  64. 'There is one apple', array('count' => 1)),
  65. 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 } %}',
  66. 'There is 5 apples (Symfony2)', array('count' => 5)),
  67. // trans filter
  68. array('{{ "Hello"|trans }}', 'Hello'),
  69. array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  70. array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  71. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  72. // transchoice filter
  73. 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)),
  74. 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%)')),
  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. }