4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\Tests\Extension
;
14 use Symfony\Bridge\Twig\Extension\TranslationExtension
;
15 use Symfony\Component\Translation\Translator
;
16 use Symfony\Component\Translation\MessageSelector
;
17 use Symfony\Component\Translation\Loader\ArrayLoader
;
18 use Symfony\Bridge\Twig\Tests\TestCase
;
20 class TranslationExtensionTest
extends TestCase
22 protected function setUp()
26 if (!class_exists('Symfony\Component\Translation\Translator')) {
27 $this->markTestSkipped('The "Translation" component is not available');
30 if (!class_exists('Twig_Environment')) {
31 $this->markTestSkipped('Twig is not available.');
35 public function testEscaping()
37 $output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
39 $this->assertEquals('Percent: 12% (approx.)', $output);
43 * @dataProvider getTransTests
45 public function testTrans($template, $expected, array $variables = array())
47 if ($expected != $this->getTemplate($template)->render($variables)) {
49 $loader = new \
Twig_Loader_Array(array('index' => $template));
50 $twig = new \
Twig_Environment($loader, array('debug' => true, 'cache' => false));
51 $twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
53 echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
54 $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
57 $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
60 public function getTransTests()
64 array('{% trans %}Hello{% endtrans %}', 'Hello'),
65 array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
67 array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
69 array('{% trans %}Hello %name%{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')),
70 array('{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
71 array('{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}', 'Hello Symfony2'),
73 array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
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 (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
82 array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
83 'There is 5 apples (Symfony2)', 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)),
88 array('{{ "Hello"|trans }}', 'Hello'),
89 array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
90 array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
91 array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
92 array('{{ "Hello"|trans({}, "messages", "fr") }}', 'Hello'),
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%\': \'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%)')),
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)),
101 public function testDefaultTranslationDomain()
105 {%- extends "base" %}
107 {%- trans_default_domain "foo" %}
110 {%- trans %}foo{% endtrans %}
111 {%- trans from "custom" %}foo{% endtrans %}
113 {{- "foo"|trans({}, "custom") }}
114 {{- "foo"|transchoice(1) }}
115 {{- "foo"|transchoice(1, {}, "custom") }}
120 {%- block content "" %}
124 $translator = new Translator('en', new MessageSelector());
125 $translator->addLoader('array', new ArrayLoader());
126 $translator->addResource('array', array('foo' => 'foo (messages)'), 'en');
127 $translator->addResource('array', array('foo' => 'foo (custom)'), 'en', 'custom');
128 $translator->addResource('array', array('foo' => 'foo (foo)'), 'en', 'foo');
130 $template = $this->getTemplate($templates, $translator);
132 $this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
135 protected function getTemplate($template, $translator = null)
137 if (null === $translator) {
138 $translator = new Translator('en', new MessageSelector());
141 if (is_array($template)) {
142 $loader = new \
Twig_Loader_Array($template);
144 $loader = new \
Twig_Loader_Array(array('index' => $template));
146 $twig = new \
Twig_Environment($loader, array('debug' => true, 'cache' => false));
147 $twig->addExtension(new TranslationExtension($translator));
149 return $twig->loadTemplate('index');