TranslationExtensionTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Bridge\Twig\Tests\Extension;
  11. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  12. use Symfony\Component\Translation\Translator;
  13. use Symfony\Component\Translation\MessageSelector;
  14. use Symfony\Component\Translation\Loader\ArrayLoader;
  15. use Symfony\Bridge\Twig\Tests\TestCase;
  16. class TranslationExtensionTest extends TestCase
  17. {
  18. protected function setUp()
  19. {
  20. parent::setUp();
  21. if (!class_exists('Symfony\Component\Translation\Translator')) {
  22. $this->markTestSkipped('The "Translation" component is not available');
  23. }
  24. if (!class_exists('Twig_Environment')) {
  25. $this->markTestSkipped('Twig is not available.');
  26. }
  27. }
  28. public function testEscaping()
  29. {
  30. $output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
  31. $this->assertEquals('Percent: 12% (approx.)', $output);
  32. }
  33. /**
  34. * @dataProvider getTransTests
  35. */
  36. public function testTrans($template, $expected, array $variables = array())
  37. {
  38. if ($expected != $this->getTemplate($template)->render($variables)) {
  39. print $template."\n";
  40. $loader = new \Twig_Loader_Array(array('index' => $template));
  41. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  42. $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  43. echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
  44. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  45. }
  46. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  47. }
  48. public function getTransTests()
  49. {
  50. return array(
  51. // trans tag
  52. array('{% trans %}Hello{% endtrans %}', 'Hello'),
  53. array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
  54. array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  55. array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
  56. array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  57. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
  58. array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
  59. // transchoice
  60. array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  61. 'There is no apples', array('count' => 0)),
  62. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  63. 'There is 5 apples', array('count' => 5)),
  64. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  65. 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
  66. array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  67. 'There is 5 apples (Symfony2)', array('count' => 5)),
  68. array('{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  69. 'There is no apples', array('count' => 0)),
  70. // trans filter
  71. array('{{ "Hello"|trans }}', 'Hello'),
  72. array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
  73. array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  74. array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
  75. array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
  76. // transchoice filter
  77. 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)),
  78. 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%)')),
  79. 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)),
  80. );
  81. }
  82. public function testDefaultTranslationDomain()
  83. {
  84. $templates = array(
  85. 'index' => '
  86. {%- extends "base" %}
  87. {%- trans_default_domain "foo" %}
  88. {%- block content %}
  89. {%- trans %}foo{% endtrans %}
  90. {%- trans from "custom" %}foo{% endtrans %}
  91. {{- "foo"|trans }}
  92. {{- "foo"|trans({}, "custom") }}
  93. {{- "foo"|transchoice(1) }}
  94. {{- "foo"|transchoice(1, {}, "custom") }}
  95. {% endblock %}
  96. ',
  97. 'base' => '
  98. {%- block content "" %}
  99. ',
  100. );
  101. $translator = new Translator('en', new MessageSelector());
  102. $translator->addLoader('array', new ArrayLoader());
  103. $translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
  104. $translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
  105. $translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
  106. $template = $this->getTemplate($templates, $translator);
  107. $this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
  108. }
  109. protected function getTemplate($template, $translator = null)
  110. {
  111. if (null === $translator) {
  112. $translator = new Translator('en', new MessageSelector());
  113. }
  114. if (is_array($template)) {
  115. $loader = new \Twig_Loader_Array($template);
  116. } else {
  117. $loader = new \Twig_Loader_Array(array('index' => $template));
  118. }
  119. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  120. $twig->addExtension(new TranslationExtension($translator));
  121. return $twig->loadTemplate('index');
  122. }
  123. }