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\Translation
;
14 use Symfony\Bridge\Twig\Extension\TranslationExtension
;
15 use Symfony\Bridge\Twig\Translation\TwigExtractor
;
16 use Symfony\Component\Translation\MessageCatalogue
;
17 use Symfony\Bridge\Twig\Tests\TestCase
;
19 class TwigExtractorTest
extends TestCase
21 protected function setUp()
23 if (!class_exists('Symfony\Component\Translation\Translator')) {
24 $this->markTestSkipped('The "Translation" component is not available');
29 * @dataProvider getExtractData
31 public function testExtract($template, $messages)
33 $loader = new \
Twig_Loader_Array(array());
34 $twig = new \
Twig_Environment($loader, array(
35 'strict_variables' => true,
38 'autoescape' => false,
40 $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface')));
42 $extractor = new TwigExtractor($twig);
43 $extractor->setPrefix('prefix');
44 $catalogue = new MessageCatalogue('en');
46 $m = new \
ReflectionMethod($extractor, 'extractTemplate');
47 $m->setAccessible(true);
48 $m->invoke($extractor, $template, $catalogue);
50 foreach ($messages as $key => $domain) {
51 $this->assertTrue($catalogue->has($key, $domain));
52 $this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
56 public function getExtractData()
59 array('{{ "new key" | trans() }}', array('new key' => 'messages')),
60 array('{{ "new key" | trans() | upper }}', array('new key' => 'messages')),
61 array('{{ "new key" | trans({}, "domain") }}', array('new key' => 'domain')),
62 array('{{ "new key" | transchoice(1) }}', array('new key' => 'messages')),
63 array('{{ "new key" | transchoice(1) | upper }}', array('new key' => 'messages')),
64 array('{{ "new key" | transchoice(1, {}, "domain") }}', array('new key' => 'domain')),
65 array('{% trans %}new key{% endtrans %}', array('new key' => 'messages')),
66 array('{% trans %} new key {% endtrans %}', array('new key' => 'messages')),
67 array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')),
68 array('{% set foo = "new key" | trans %}', array('new key' => 'messages')),
69 array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')),
71 // make sure 'trans_default_domain' tag is supported
72 array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')),
73 array('{% trans_default_domain "domain" %}{{ "new key"|transchoice }}', array('new key' => 'domain')),
74 array('{% trans_default_domain "domain" %}{% trans %}new key{% endtrans %}', array('new key' => 'domain')),
76 // make sure this works with twig's named arguments
77 array('{{ "new key" | trans(domain="domain") }}', array('new key' => 'domain')),
78 array('{{ "new key" | transchoice(domain="domain", count=1) }}', array('new key' => 'domain')),