]> git.immae.eu Git - github/wallabag/wallabag.git/blame - vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php
gitignore vendor
[github/wallabag/wallabag.git] / vendor / symfony / twig-bridge / Symfony / Bridge / Twig / Tests / Extension / TranslationExtensionTest.php
CommitLineData
4f5b44bd
NL
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Bridge\Twig\Tests\Extension;
13
14use Symfony\Bridge\Twig\Extension\TranslationExtension;
15use Symfony\Component\Translation\Translator;
16use Symfony\Component\Translation\MessageSelector;
17use Symfony\Component\Translation\Loader\ArrayLoader;
18use Symfony\Bridge\Twig\Tests\TestCase;
19
20class TranslationExtensionTest extends TestCase
21{
22 protected function setUp()
23 {
24 parent::setUp();
25
26 if (!class_exists('Symfony\Component\Translation\Translator')) {
27 $this->markTestSkipped('The "Translation" component is not available');
28 }
29
30 if (!class_exists('Twig_Environment')) {
31 $this->markTestSkipped('Twig is not available.');
32 }
33 }
34
35 public function testEscaping()
36 {
37 $output = $this->getTemplate('{% trans %}Percent: %value%%% (%msg%){% endtrans %}')->render(array('value' => 12, 'msg' => 'approx.'));
38
39 $this->assertEquals('Percent: 12% (approx.)', $output);
40 }
41
42 /**
43 * @dataProvider getTransTests
44 */
45 public function testTrans($template, $expected, array $variables = array())
46 {
47 if ($expected != $this->getTemplate($template)->render($variables)) {
48 print $template."\n";
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())));
52
53 echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n";
54 $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
55 }
56
57 $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
58 }
59
60 public function getTransTests()
61 {
62 return array(
63 // trans tag
64 array('{% trans %}Hello{% endtrans %}', 'Hello'),
65 array('{% trans %}%name%{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')),
66
67 array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'),
68
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'),
72
73 array('{% trans into "fr"%}Hello{% endtrans %}', 'Hello'),
74
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 (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)),
86
87 // trans filter
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'),
93
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%\': \'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)),
98 );
99 }
100
101 public function testDefaultTranslationDomain()
102 {
103 $templates = array(
104 'index' => '
105 {%- extends "base" %}
106
107 {%- trans_default_domain "foo" %}
108
109 {%- block content %}
110 {%- trans %}foo{% endtrans %}
111 {%- trans from "custom" %}foo{% endtrans %}
112 {{- "foo"|trans }}
113 {{- "foo"|trans({}, "custom") }}
114 {{- "foo"|transchoice(1) }}
115 {{- "foo"|transchoice(1, {}, "custom") }}
116 {% endblock %}
117 ',
118
119 'base' => '
120 {%- block content "" %}
121 ',
122 );
123
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');
129
130 $template = $this->getTemplate($templates, $translator);
131
132 $this->assertEquals('foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)', trim($template->render(array())));
133 }
134
135 protected function getTemplate($template, $translator = null)
136 {
137 if (null === $translator) {
138 $translator = new Translator('en', new MessageSelector());
139 }
140
141 if (is_array($template)) {
142 $loader = new \Twig_Loader_Array($template);
143 } else {
144 $loader = new \Twig_Loader_Array(array('index' => $template));
145 }
146 $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
147 $twig->addExtension(new TranslationExtension($translator));
148
149 return $twig->loadTemplate('index');
150 }
151}