TranslationExtensionTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  13. use Symfony\Component\Translation\Translator;
  14. use Symfony\Component\Translation\MessageSelector;
  15. use Symfony\Component\Translation\Loader\ArrayLoader;
  16. use Twig\Environment;
  17. use Twig\Loader\ArrayLoader as TwigArrayLoader;
  18. class TranslationExtensionTest extends TestCase
  19. {
  20. public function testEscaping()
  21. {
  22. $output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
  23. $this->assertEquals('Percent: 12% (approx.)', $output);
  24. }
  25. /**
  26. * @dataProvider getTransTests
  27. */
  28. public function testTrans($template, $expected, array $variables = array())
  29. {
  30. if ($expected != $this->getTemplate($template)->render($variables)) {
  31. echo $template."\n";
  32. $loader = new TwigArrayLoader(array('index' => $template));
  33. $twig = new Environment($loader, array('debug' => true, 'cache' => false));
  34. $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
  35. echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
  36. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  37. }
  38. $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
  39. }
  40. /**
  41. * @expectedException \Twig\Error\SyntaxError
  42. * @expectedExceptionMessage Unexpected token. Twig was looking for the "with", "from", or "into" keyword in "index" at line 3.
  43. */
  44. public function testTransUnknownKeyword()
  45. {
  46. $output = $this->getTemplate("{% trans \n\nfoo %}{% endtrans %}")->render();
  47. }
  48. /**
  49. * @expectedException \Twig\Error\SyntaxError
  50. * @expectedExceptionMessage A message inside a trans tag must be a simple text in "index" at line 2.
  51. */
  52. public function testTransComplexBody()
  53. {
  54. $output = $this->getTemplate("{% trans %}\n{{ 1 + 2 }}{% endtrans %}")->render();
  55. }
  56. /**
  57. * @expectedException \Twig\Error\SyntaxError
  58. * @expectedExceptionMessage A message inside a transchoice tag must be a simple text in "index" at line 2.
  59. */
  60. public function testTransChoiceComplexBody()
  61. {
  62. $output = $this->getTemplate("{% transchoice count %}\n{{ 1 + 2 }}{% endtranschoice %}")->render();
  63. }
  64. public function getTransTests()
  65. {
  66. return array(
  67. // trans tag
  68. array('{% trans %}Hello{% endtrans %}', 'Hello'),
  69. array('{% trans %}%name%{% endtrans %}', 'Symfony', array('name' => 'Symfony')),
  70. array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
  71. array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony', array('name' => 'Symfony')),
  72. array('{% trans with { \'%name%\': \'Symfony\' } %}Hello %name%{% endtrans %}', 'Hello Symfony'),
  73. array('{% set vars = { \'%name%\': \'Symfony\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony'),
  74. array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
  75. // transchoice
  76. array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  77. 'There is no apples', array('count' => 0)),
  78. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  79. 'There is 5 apples', array('count' => 5)),
  80. array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  81. 'There is 5 apples (Symfony)', array('count' => 5, 'name' => 'Symfony')),
  82. array('{% transchoice count with { \'%name%\': \'Symfony\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
  83. 'There is 5 apples (Symfony)', array('count' => 5)),
  84. array('{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  85. 'There is no apples', array('count' => 0)),
  86. array('{% transchoice 5 into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}',
  87. 'There is 5 apples'),
  88. // trans filter
  89. array('{{ "Hello"|trans }}', 'Hello'),
  90. array('{{ name|trans }}', 'Symfony', array('name' => 'Symfony')),
  91. array('{{ hello|trans({ \'%name%\': \'Symfony\' }) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
  92. array('{% set vars = { \'%name%\': \'Symfony\' } %}{{ hello|trans(vars) }}', 'Hello Symfony', array('hello' => 'Hello %name%')),
  93. array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
  94. // transchoice filter
  95. 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)),
  96. array('{{ text|transchoice(5, {\'%name%\': \'Symfony\'}) }}', 'There is 5 apples (Symfony)', array('text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)')),
  97. 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)),
  98. );
  99. }
  100. public function testDefaultTranslationDomain()
  101. {
  102. $templates = array(
  103. 'index' => '
  104. {%- extends "base" %}
  105. {%- trans_default_domain "foo" %}
  106. {%- block content %}
  107. {%- trans %}foo{% endtrans %}
  108. {%- trans from "custom" %}foo{% endtrans %}
  109. {{- "foo"|trans }}
  110. {{- "foo"|trans({}, "custom") }}
  111. {{- "foo"|transchoice(1) }}
  112. {{- "foo"|transchoice(1, {}, "custom") }}
  113. {% endblock %}
  114. ',
  115. 'base' => '
  116. {%- block content "" %}
  117. ',
  118. );
  119. $translator = new Translator('en', new MessageSelector());
  120. $translator->addLoader('array', new ArrayLoader());
  121. $translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
  122. $translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
  123. $translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
  124. $template = $this->getTemplate($templates, $translator);
  125. $this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
  126. }
  127. public function testDefaultTranslationDomainWithNamedArguments()
  128. {
  129. $templates = array(
  130. 'index' => '
  131. {%- trans_default_domain "foo" %}
  132. {%- block content %}
  133. {{- "foo"|trans(arguments = {}, domain = "custom") }}
  134. {{- "foo"|transchoice(count = 1) }}
  135. {{- "foo"|transchoice(count = 1, arguments = {}, domain = "custom") }}
  136. {{- "foo"|trans({}, domain = "custom") }}
  137. {{- "foo"|trans({}, "custom", locale = "fr") }}
  138. {{- "foo"|transchoice(1, arguments = {}, domain = "custom") }}
  139. {{- "foo"|transchoice(1, {}, "custom", locale = "fr") }}
  140. {% endblock %}
  141. ',
  142. 'base' => '
  143. {%- block content "" %}
  144. ',
  145. );
  146. $translator = new Translator('en', new MessageSelector());
  147. $translator->addLoader('array', new ArrayLoader());
  148. $translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
  149. $translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
  150. $translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
  151. $translator->addResource('array', array('foo' => 'foo (fr)'), 'fr', 'custom');
  152. $template = $this->getTemplate($templates, $translator);
  153. $this->assertEquals('foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)', trim($template->render(array())));
  154. }
  155. protected function getTemplate($template, $translator = null)
  156. {
  157. if (null === $translator) {
  158. $translator = new Translator('en', new MessageSelector());
  159. }
  160. if (is_array($template)) {
  161. $loader = new TwigArrayLoader($template);
  162. } else {
  163. $loader = new TwigArrayLoader(array('index' => $template));
  164. }
  165. $twig = new Environment($loader, array('debug' => true, 'cache' => false));
  166. $twig->addExtension(new TranslationExtension($translator));
  167. return $twig->loadTemplate('index');
  168. }
  169. }