]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | namespace Symfony\Bridge\Twig\Tests\Translation; | |
13 | ||
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; | |
18 | ||
19 | class TwigExtractorTest extends TestCase | |
20 | { | |
21 | protected function setUp() | |
22 | { | |
23 | if (!class_exists('Symfony\Component\Translation\Translator')) { | |
24 | $this->markTestSkipped('The "Translation" component is not available'); | |
25 | } | |
26 | } | |
27 | ||
28 | /** | |
29 | * @dataProvider getExtractData | |
30 | */ | |
31 | public function testExtract($template, $messages) | |
32 | { | |
33 | $loader = new \Twig_Loader_Array(array()); | |
34 | $twig = new \Twig_Environment($loader, array( | |
35 | 'strict_variables' => true, | |
36 | 'debug' => true, | |
37 | 'cache' => false, | |
38 | 'autoescape' => false, | |
39 | )); | |
40 | $twig->addExtension(new TranslationExtension($this->getMock('Symfony\Component\Translation\TranslatorInterface'))); | |
41 | ||
42 | $extractor = new TwigExtractor($twig); | |
43 | $extractor->setPrefix('prefix'); | |
44 | $catalogue = new MessageCatalogue('en'); | |
45 | ||
46 | $m = new \ReflectionMethod($extractor, 'extractTemplate'); | |
47 | $m->setAccessible(true); | |
48 | $m->invoke($extractor, $template, $catalogue); | |
49 | ||
50 | foreach ($messages as $key => $domain) { | |
51 | $this->assertTrue($catalogue->has($key, $domain)); | |
52 | $this->assertEquals('prefix'.$key, $catalogue->get($key, $domain)); | |
53 | } | |
54 | } | |
55 | ||
56 | public function getExtractData() | |
57 | { | |
58 | return array( | |
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')), | |
70 | ||
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')), | |
75 | ||
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')), | |
79 | ); | |
80 | } | |
81 | } |