|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Bundle\TwigBundle\Tests; |
| 13 | + |
| 14 | +use Symfony\Bundle\TwigBundle\Tests\TestCase; |
| 15 | +use Symfony\Bundle\TwigBundle\Extension\TransExtension; |
| 16 | +use Symfony\Component\Translation\Translator; |
| 17 | +use Symfony\Component\Translation\MessageSelector; |
| 18 | + |
| 19 | +class TransTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @dataProvider getTransTests |
| 23 | + */ |
| 24 | + public function testTrans($template, $expected, array $variables = array()) |
| 25 | + { |
| 26 | + |
| 27 | + if ($expected != $this->getTemplate($template)->render($variables)) { |
| 28 | + print $template."\n"; |
| 29 | + $loader = new \Twig_Loader_Array(array('index' => $template)); |
| 30 | + $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false)); |
| 31 | +
9E7A
$twig->addExtension(new TransExtension(new Translator('en', new MessageSelector()))); |
| 32 | + |
| 33 | + echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSource('index'), 'index')))."\n\n"; |
| 34 | + $this->assertEquals($expected, $this->getTemplate($template)->render($variables)); |
| 35 | + exit(); |
| 36 | + } |
| 37 | + |
| 38 | + $this->assertEquals($expected, $this->getTemplate($template)->render($variables)); |
| 39 | + } |
| 40 | + |
| 41 | + public function getTransTests() |
| 42 | + { |
| 43 | + return array( |
| 44 | + // trans tag |
| 45 | + array('{% trans "Hello" %}', 'Hello'), |
| 46 | + array('{% trans name %}', 'Symfony2', array('name' => 'Symfony2')), |
| 47 | + array('{% trans hello with [\'{{ name }}\': \'Symfony2\'] %}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')), |
| 48 | + array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{% trans hello with vars %}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')), |
| 49 | + |
| 50 | + array('{% trans %}Hello{% endtrans %}', 'Hello'), |
| 51 | + array('{% trans %}{{ name }}{% endtrans %}', 'Symfony2', array('name' => 'Symfony2')), |
| 52 | + |
| 53 | + array('{% trans "Hello" from elsewhere %}', 'Hello'), |
| 54 | + array('{% trans from elsewhere %}Hello{% endtrans %}', 'Hello'), |
| 55 | + |
| 56 | + array('{% trans %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2', array('name' => 'Symfony2')), |
| 57 | + array('{% trans with [\'{{ name }}\': \'Symfony2\'] %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2'), |
| 58 | + array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{% trans with vars %}Hello {{ name }}{% endtrans %}', 'Hello Symfony2'), |
| 59 | + |
| 60 | + // transchoice |
| 61 | + array('{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples{% endtranschoice %}', |
| 62 | + 'There is no apples', array('count' => 0)), |
| 63 | + array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples{% endtranschoice %}', |
| 64 | + 'There is 5 apples', array('count' => 5)), |
| 65 | + array('{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples ({{ name }}){% endtranschoice %}', |
| 66 | + 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')), |
| 67 | + array('{% transchoice count with [\'{{ name }}\': \'Symfony2\'] %}{0} There is no apples|{1} There is one apple|]1,Inf] There is {{ count }} apples ({{ name }}){% endtranschoice %}', |
| 68 | + 'There is 5 apples (Symfony2)', array('count' => 5)), |
| 69 | + |
| 70 | + // trans filter |
| 71 | + array('{{ "Hello"|trans }}', 'Hello'), |
| 72 | + array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')), |
| 73 | + array('{{ hello|trans([\'{{ name }}\': \'Symfony2\']) }}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')), |
| 74 | + array('{% set vars = [\'{{ name }}\': \'Symfony2\'] %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello {{ name }}')), |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + protected function getTemplate($template) |
| 79 | + { |
| 80 | + $loader = new \Twig_Loader_Array(array('index' => $template)); |
| 81 | + $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false)); |
| 82 | + $twig->addExtension(new TransExtension(new Translator('en', new MessageSelector()))); |
| 83 | + |
| 84 | + return $twig->loadTemplate('index'); |
| 85 | + } |
| 86 | +} |
0 commit comments