2010-10-02 08:39:48 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-10-02 08:39:48 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2011-03-28 15:13:30 +01:00
namespace Symfony\Tests\Bridge\Twig\Extension ;
2010-10-02 08:39:48 +01:00
2011-03-28 15:13:30 +01:00
use Symfony\Bridge\Twig\Extension\TranslationExtension ;
2010-10-02 08:39:48 +01:00
use Symfony\Component\Translation\Translator ;
use Symfony\Component\Translation\MessageSelector ;
2011-03-28 15:13:30 +01:00
use Symfony\Tests\Bridge\Twig\TestCase ;
2010-10-02 08:39:48 +01:00
2011-03-28 15:13:30 +01:00
class TranslationExtensionTest extends TestCase
2010-10-02 08:39:48 +01:00
{
2011-03-23 14:23:52 +00:00
protected function setUp ()
{
if ( ! class_exists ( 'Twig_Environment' )) {
$this -> markTestSkipped ( 'Twig is not available.' );
}
}
2010-10-02 08:39:48 +01:00
/**
* @ dataProvider getTransTests
*/
public function testTrans ( $template , $expected , array $variables = array ())
{
if ( $expected != $this -> getTemplate ( $template ) -> render ( $variables )) {
print $template . " \n " ;
$loader = new \Twig_Loader_Array ( array ( 'index' => $template ));
$twig = new \Twig_Environment ( $loader , array ( 'debug' => true , 'cache' => false ));
2011-03-28 15:13:30 +01:00
$twig -> addExtension ( new TranslationExtension ( new Translator ( 'en' , new MessageSelector ())));
2010-10-02 08:39:48 +01:00
echo $twig -> compile ( $twig -> parse ( $twig -> tokenize ( $twig -> getLoader () -> getSource ( 'index' ), 'index' ))) . " \n \n " ;
$this -> assertEquals ( $expected , $this -> getTemplate ( $template ) -> render ( $variables ));
}
$this -> assertEquals ( $expected , $this -> getTemplate ( $template ) -> render ( $variables ));
}
public function getTransTests ()
{
return array (
// trans tag
array ( '{% trans "Hello" %}' , 'Hello' ),
2011-01-03 18:50:23 +00:00
array ( '{% trans "Hello %name%" %}' , 'Hello Symfony2' , array ( 'name' => 'Symfony2' )),
2010-10-02 08:39:48 +01:00
array ( '{% trans name %}' , 'Symfony2' , array ( 'name' => 'Symfony2' )),
2011-01-03 18:50:23 +00:00
array ( '{% trans hello with { \'%name%\': \'Symfony2\' } %}' , 'Hello Symfony2' , array ( 'hello' => 'Hello %name%' )),
array ( '{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans hello with vars %}' , 'Hello Symfony2' , array ( 'hello' => 'Hello %name%' )),
2010-10-02 08:39:48 +01:00
array ( '{% trans %}Hello{% endtrans %}' , 'Hello' ),
2011-01-03 18:50:23 +00:00
array ( '{% trans %}%name%{% endtrans %}' , 'Symfony2' , array ( 'name' => 'Symfony2' )),
2010-10-02 08:39:48 +01:00
array ( '{% trans "Hello" from elsewhere %}' , 'Hello' ),
array ( '{% trans from elsewhere %}Hello{% endtrans %}' , 'Hello' ),
2011-01-03 18:50:23 +00:00
array ( '{% trans %}Hello %name%{% endtrans %}' , 'Hello Symfony2' , array ( 'name' => 'Symfony2' )),
array ( '{% trans with { \'%name%\': \'Symfony2\' } %}Hello %name%{% endtrans %}' , 'Hello Symfony2' ),
array ( '{% set vars = { \'%name%\': \'Symfony2\' } %}{% trans with vars %}Hello %name%{% endtrans %}' , 'Hello Symfony2' ),
2010-10-02 08:39:48 +01:00
// transchoice
2011-01-03 18:50:23 +00:00
array ( '{% transchoice count from "messages" %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}' ,
2010-10-02 08:39:48 +01:00
'There is no apples' , array ( 'count' => 0 )),
2011-01-03 18:50:23 +00:00
array ( '{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}' ,
2010-10-02 08:39:48 +01:00
'There is 5 apples' , array ( 'count' => 5 )),
2011-01-03 18:50:23 +00:00
array ( '{% transchoice count %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}' ,
2010-10-02 08:39:48 +01:00
'There is 5 apples (Symfony2)' , array ( 'count' => 5 , 'name' => 'Symfony2' )),
2011-01-03 18:50:23 +00:00
array ( '{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}' ,
2010-10-02 08:39:48 +01:00
'There is 5 apples (Symfony2)' , array ( 'count' => 5 )),
2011-04-12 10:15:19 +01:00
array ( '{% transchoice count for "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples" from "messages" %}' ,
'There is one apple' , array ( 'count' => 1 )),
array ( '{% set text = "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)" %}{% transchoice count for text with { \'%name%\': \'Symfony2\', \'%count%\': count } %}' ,
'There is 5 apples (Symfony2)' , array ( 'count' => 5 )),
2010-10-02 08:39:48 +01:00
// trans filter
array ( '{{ "Hello"|trans }}' , 'Hello' ),
array ( '{{ name|trans }}' , 'Symfony2' , array ( 'name' => 'Symfony2' )),
2011-01-03 18:50:23 +00:00
array ( '{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}' , 'Hello Symfony2' , array ( 'hello' => 'Hello %name%' )),
array ( '{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}' , 'Hello Symfony2' , array ( 'hello' => 'Hello %name%' )),
2011-04-12 10:10:55 +01:00
// transchoice filter
2011-04-21 08:01:11 +01:00
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 )),
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%)' )),
2010-10-02 08:39:48 +01:00
);
}
protected function getTemplate ( $template )
{
$loader = new \Twig_Loader_Array ( array ( 'index' => $template ));
$twig = new \Twig_Environment ( $loader , array ( 'debug' => true , 'cache' => false ));
2011-03-28 15:13:30 +01:00
$twig -> addExtension ( new TranslationExtension ( new Translator ( 'en' , new MessageSelector ())));
2010-10-02 08:39:48 +01:00
return $twig -> loadTemplate ( 'index' );
}
}