TranslationExtensionTest.php 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // trans filter
  58. array('{{ "Hello"|trans }}', 'Hello'),
  59. array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  60. array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  61. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  62. // transchoice filter
  63. 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)),
  64. 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%)')),
  65. );
  66. }
  67. protected function getTemplate($template)
  68. {
  69. $loader = new \Twig_Loader_Array(array('index' => $template));
  70. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  71. $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  72. return $twig->loadTemplate('index');
  73. }
  74. }