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 .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Bridge\Twig\Tests\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 ;
2012-01-02 16:47:49 +00:00
use Symfony\Component\Translation\Loader\ArrayLoader ;
2010-10-02 08:39:48 +01:00
2013-09-17 15:25:42 +01:00
class TranslationExtensionTest extends \PHPUnit_Framework_TestCase
2010-10-02 08:39:48 +01:00
{
2011-05-25 10:28:10 +01:00
public function testEscaping ()
{
$output = $this -> getTemplate ( '{% trans %}Percent: %value%%% (%msg%){% endtrans %}' ) -> render ( array ( 'value' => 12 , 'msg' => 'approx.' ));
$this -> assertEquals ( 'Percent: 12% (approx.)' , $output );
}
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 ));
}
2014-01-25 09:45:58 +00:00
/**
* @ expectedException \Twig_Error_Syntax
* @ expectedExceptionMessage Unexpected token . Twig was looking for the " with " , " from " , or " into " keyword in " index " at line 3.
*/
public function testTransUnknownKeyword ()
{
$output = $this -> getTemplate ( " { % trans \n \n foo %} { % endtrans %} " ) -> render ();
}
/**
* @ expectedException \Twig_Error_Syntax
* @ expectedExceptionMessage A message inside a trans tag must be a simple text in " index " at line 2.
*/
public function testTransComplexBody ()
{
$output = $this -> getTemplate ( " { % trans %} \n { { 1 + 2 }} { % endtrans %} " ) -> render ();
}
/**
* @ expectedException \Twig_Error_Syntax
* @ expectedExceptionMessage A message inside a transchoice tag must be a simple text in " index " at line 2.
*/
public function testTransChoiceComplexBody ()
{
$output = $this -> getTemplate ( " { % transchoice count %} \n { { 1 + 2 }} { % endtranschoice %} " ) -> render ();
}
2010-10-02 08:39:48 +01:00
public function getTransTests ()
{
return array (
// trans tag
array ( '{% trans %}Hello{% endtrans %}' , 'Hello' ),
2014-11-24 12:25:40 +00:00
array ( '{% trans %}%name%{% endtrans %}' , 'Symfony' , array ( 'name' => 'Symfony' )),
2010-10-02 08:39:48 +01:00
array ( '{% trans from elsewhere %}Hello{% endtrans %}' , 'Hello' ),
2014-11-24 12:25:40 +00:00
array ( '{% trans %}Hello %name%{% endtrans %}' , 'Hello Symfony' , array ( 'name' => 'Symfony' )),
array ( '{% trans with { \'%name%\': \'Symfony\' } %}Hello %name%{% endtrans %}' , 'Hello Symfony' ),
array ( '{% set vars = { \'%name%\': \'Symfony\' } %}{% trans with vars %}Hello %name%{% endtrans %}' , 'Hello Symfony' ),
2010-10-02 08:39:48 +01:00
2011-07-26 00:27:09 +01:00
array ( '{% trans into "fr"%}Hello{% endtrans %}' , 'Hello' ),
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 %}' ,
2014-09-21 19:53:12 +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 %}' ,
2014-09-21 19:53:12 +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 %}' ,
2014-11-24 12:25:40 +00:00
'There is 5 apples (Symfony)' , array ( 'count' => 5 , 'name' => 'Symfony' ),),
array ( '{% transchoice count with { \'%name%\': \'Symfony\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}' ,
'There is 5 apples (Symfony)' , array ( 'count' => 5 ),),
2011-07-26 00:27:09 +01:00
array ( '{% transchoice count into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}' ,
2014-09-21 19:53:12 +01:00
'There is no apples' , array ( 'count' => 0 ),),
2014-05-01 19:10:47 +01:00
array ( '{% transchoice 5 into "fr"%}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples{% endtranschoice %}' ,
2014-09-21 19:53:12 +01:00
'There is 5 apples' ,),
2010-10-02 08:39:48 +01:00
// trans filter
array ( '{{ "Hello"|trans }}' , 'Hello' ),
2014-11-24 12:25:40 +00:00
array ( '{{ name|trans }}' , 'Symfony' , array ( 'name' => 'Symfony' )),
array ( '{{ hello|trans({ \'%name%\': \'Symfony\' }) }}' , 'Hello Symfony' , array ( 'hello' => 'Hello %name%' )),
array ( '{% set vars = { \'%name%\': \'Symfony\' } %}{{ hello|trans(vars) }}' , 'Hello Symfony' , array ( 'hello' => 'Hello %name%' )),
2011-07-26 00:25:25 +01:00
array ( '{{ "Hello"|trans({}, "messages", "fr") }}' , 'Hello' ),
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 )),
2014-11-24 12:25:40 +00:00
array ( '{{ text|transchoice(5, {\'%name%\': \'Symfony\'}) }}' , 'There is 5 apples (Symfony)' , array ( 'text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)' )),
2011-07-26 00:25:25 +01:00
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 )),
2010-10-02 08:39:48 +01:00
);
}
2012-01-02 16:47:49 +00:00
public function testDefaultTranslationDomain ()
2010-10-02 08:39:48 +01:00
{
2012-01-02 16:47:49 +00:00
$templates = array (
'index' => '
{ %- extends " base " % }
{ %- trans_default_domain " foo " % }
{ %- block content % }
{ %- trans % } foo { % endtrans % }
{ %- trans from " custom " % } foo { % endtrans % }
{{ - " foo " | trans }}
{{ - " foo " | trans ({}, " custom " ) }}
{{ - " foo " | transchoice ( 1 ) }}
{{ - " foo " | transchoice ( 1 , {}, " custom " ) }}
{ % endblock % }
' ,
'base' => '
{ %- block content " " % }
' ,
);
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (messages)' ), 'en' );
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (custom)' ), 'en' , 'custom' );
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (foo)' ), 'en' , 'foo' );
$template = $this -> getTemplate ( $templates , $translator );
$this -> assertEquals ( 'foo (foo)foo (custom)foo (foo)foo (custom)foo (foo)foo (custom)' , trim ( $template -> render ( array ())));
}
2015-01-07 12:58:10 +00:00
public function testDefaultTranslationDomainWithNamedArguments ()
{
$templates = array (
'index' => '
{ %- trans_default_domain " foo " % }
{ %- block content % }
{{ - " foo " | trans ( arguments = {}, domain = " custom " ) }}
{{ - " foo " | transchoice ( count = 1 ) }}
{{ - " foo " | transchoice ( count = 1 , arguments = {}, domain = " custom " ) }}
{{ - " foo " | trans ({}, domain = " custom " ) }}
{{ - " foo " | trans ({}, " custom " , locale = " fr " ) }}
{{ - " foo " | transchoice ( 1 , arguments = {}, domain = " custom " ) }}
{{ - " foo " | transchoice ( 1 , {}, " custom " , locale = " fr " ) }}
{ % endblock % }
' ,
'base' => '
{ %- block content " " % }
' ,
);
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (messages)' ), 'en' );
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (custom)' ), 'en' , 'custom' );
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (foo)' ), 'en' , 'foo' );
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (fr)' ), 'fr' , 'custom' );
$template = $this -> getTemplate ( $templates , $translator );
$this -> assertEquals ( 'foo (custom)foo (foo)foo (custom)foo (custom)foo (fr)foo (custom)foo (fr)' , trim ( $template -> render ( array ())));
}
2012-01-02 16:47:49 +00:00
protected function getTemplate ( $template , $translator = null )
{
if ( null === $translator ) {
$translator = new Translator ( 'en' , new MessageSelector ());
}
if ( is_array ( $template )) {
$loader = new \Twig_Loader_Array ( $template );
} else {
$loader = new \Twig_Loader_Array ( array ( 'index' => $template ));
}
2010-10-02 08:39:48 +01:00
$twig = new \Twig_Environment ( $loader , array ( 'debug' => true , 'cache' => false ));
2012-01-02 16:47:49 +00:00
$twig -> addExtension ( new TranslationExtension ( $translator ));
2010-10-02 08:39:48 +01:00
return $twig -> loadTemplate ( 'index' );
}
}