2010-09-27 08:45:29 +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-09-27 08:45:29 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Component\Translation ;
use Symfony\Component\Translation\Translator ;
2010-09-27 15:53:23 +01:00
use Symfony\Component\Translation\MessageSelector ;
2010-09-27 08:45:29 +01:00
use Symfony\Component\Translation\Loader\ArrayLoader ;
class TranslatorTest extends \PHPUnit_Framework_TestCase
{
2010-10-31 21:33:08 +00:00
public function testSetGetLocale ()
{
$translator = new Translator ( 'en' , new MessageSelector ());
$this -> assertEquals ( 'en' , $translator -> getLocale ());
$translator -> setLocale ( 'fr' );
$this -> assertEquals ( 'fr' , $translator -> getLocale ());
}
public function testSetFallbackLocale ()
{
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foofoo' ), 'en' );
$translator -> addResource ( 'array' , array ( 'bar' => 'foobar' ), 'fr' );
// force catalogue loading
$translator -> trans ( 'bar' );
$translator -> setFallbackLocale ( 'fr' );
$this -> assertEquals ( 'foobar' , $translator -> trans ( 'bar' ));
}
2011-09-15 07:19:52 +01:00
public function testSetFallbackLocaleMultiple ()
{
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (en)' ), 'en' );
$translator -> addResource ( 'array' , array ( 'bar' => 'bar (fr)' ), 'fr' );
// force catalogue loading
$translator -> trans ( 'bar' );
$translator -> setFallbackLocale ( array ( 'fr_FR' , 'fr' ));
$this -> assertEquals ( 'bar (fr)' , $translator -> trans ( 'bar' ));
}
2010-10-31 21:33:08 +00:00
public function testTransWithFallbackLocale ()
{
2011-02-27 11:12:45 +00:00
$translator = new Translator ( 'fr_FR' , new MessageSelector ());
2010-10-31 21:33:08 +00:00
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foofoo' ), 'en_US' );
$translator -> addResource ( 'array' , array ( 'bar' => 'foobar' ), 'en' );
2011-02-27 11:12:45 +00:00
$translator -> setFallbackLocale ( 'en' );
2010-10-31 21:33:08 +00:00
$this -> assertEquals ( 'foobar' , $translator -> trans ( 'bar' ));
}
2011-02-27 21:18:34 +00:00
public function testTransWithFallbackLocaleBis ()
{
$translator = new Translator ( 'en_US' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foofoo' ), 'en_US' );
$translator -> addResource ( 'array' , array ( 'bar' => 'foobar' ), 'en' );
$this -> assertEquals ( 'foobar' , $translator -> trans ( 'bar' ));
}
2011-09-15 07:19:52 +01:00
public function testTransWithFallbackLocaleTer ()
{
$translator = new Translator ( 'fr_FR' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foo (en_US)' ), 'en_US' );
$translator -> addResource ( 'array' , array ( 'bar' => 'bar (en)' ), 'en' );
$translator -> setFallbackLocale ( array ( 'en_US' , 'en' ));
$this -> assertEquals ( 'foo (en_US)' , $translator -> trans ( 'foo' ));
$this -> assertEquals ( 'bar (en)' , $translator -> trans ( 'bar' ));
}
2011-09-21 16:23:52 +01:00
public function testTransNonExistentWithFallback ()
{
$translator = new Translator ( 'fr' , new MessageSelector ());
$translator -> setFallbackLocale ( 'en' );
$translator -> addLoader ( 'array' , new ArrayLoader ());
$this -> assertEquals ( 'non-existent' , $translator -> trans ( 'non-existent' ));
}
2010-10-31 21:33:08 +00:00
/**
* @ expectedException RuntimeException
*/
public function testWhenAResourceHasNoRegisteredLoader ()
{
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addResource ( 'array' , array ( 'foo' => 'foofoo' ), 'en' );
$translator -> trans ( 'foo' );
}
2010-09-27 08:45:29 +01:00
/**
* @ dataProvider getTransTests
*/
public function testTrans ( $expected , $id , $translation , $parameters , $locale , $domain )
{
2010-09-27 15:53:23 +01:00
$translator = new Translator ( 'en' , new MessageSelector ());
2010-09-27 08:45:29 +01:00
$translator -> addLoader ( 'array' , new ArrayLoader ());
2011-04-24 08:39:28 +01:00
$translator -> addResource ( 'array' , array (( string ) $id => $translation ), $locale , $domain );
2010-09-27 08:45:29 +01:00
$this -> assertEquals ( $expected , $translator -> trans ( $id , $parameters , $domain , $locale ));
}
2010-11-14 19:31:11 +00:00
/**
* @ dataProvider getFlattenedTransTests
*/
public function testFlattenedTrans ( $expected , $messages , $id )
{
$translator = new Translator ( 'en' , new MessageSelector ());
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , $messages , 'fr' , '' );
$this -> assertEquals ( $expected , $translator -> trans ( $id , array (), '' , 'fr' ));
}
2010-09-27 08:45:29 +01:00
/**
* @ dataProvider getTransChoiceTests
*/
public function testTransChoice ( $expected , $id , $translation , $number , $parameters , $locale , $domain )
{
2010-09-27 15:53:23 +01:00
$translator = new Translator ( 'en' , new MessageSelector ());
2010-09-27 08:45:29 +01:00
$translator -> addLoader ( 'array' , new ArrayLoader ());
2011-04-24 08:39:28 +01:00
$translator -> addResource ( 'array' , array (( string ) $id => $translation ), $locale , $domain );
2010-09-27 08:45:29 +01:00
$this -> assertEquals ( $expected , $translator -> transChoice ( $id , $number , $parameters , $domain , $locale ));
}
public function getTransTests ()
{
return array (
array ( 'Symfony2 est super !' , 'Symfony2 is great!' , 'Symfony2 est super !' , array (), 'fr' , '' ),
array ( 'Symfony2 est awesome !' , 'Symfony2 is %what%!' , 'Symfony2 est %what% !' , array ( '%what%' => 'awesome' ), 'fr' , '' ),
2011-04-24 08:39:28 +01:00
array ( 'Symfony2 est super !' , new String ( 'Symfony2 is great!' ), 'Symfony2 est super !' , array (), 'fr' , '' ),
2010-09-27 08:45:29 +01:00
);
}
2010-11-14 19:31:11 +00:00
public function getFlattenedTransTests ()
{
$messages = array (
'symfony2' => array (
'is' => array (
'great' => 'Symfony2 est super!'
)
),
'foo' => array (
'bar' => array (
'baz' => 'Foo Bar Baz'
),
'baz' => 'Foo Baz' ,
),
);
return array (
array ( 'Symfony2 est super!' , $messages , 'symfony2.is.great' ),
array ( 'Foo Bar Baz' , $messages , 'foo.bar.baz' ),
array ( 'Foo Baz' , $messages , 'foo.baz' ),
);
}
2010-09-27 08:45:29 +01:00
public function getTransChoiceTests ()
{
return array (
array ( 'Il y a 0 pomme' , '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples' , '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes' , 0 , array ( '%count%' => 0 ), 'fr' , '' ),
array ( 'Il y a 1 pomme' , '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples' , '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes' , 1 , array ( '%count%' => 1 ), 'fr' , '' ),
array ( 'Il y a 10 pommes' , '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples' , '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes' , 10 , array ( '%count%' => 10 ), 'fr' , '' ),
array ( 'Il y a 0 pomme' , 'There is one apple|There is %count% apples' , 'Il y a %count% pomme|Il y a %count% pommes' , 0 , array ( '%count%' => 0 ), 'fr' , '' ),
array ( 'Il y a 1 pomme' , 'There is one apple|There is %count% apples' , 'Il y a %count% pomme|Il y a %count% pommes' , 1 , array ( '%count%' => 1 ), 'fr' , '' ),
array ( 'Il y a 10 pommes' , 'There is one apple|There is %count% apples' , 'Il y a %count% pomme|Il y a %count% pommes' , 10 , array ( '%count%' => 10 ), 'fr' , '' ),
array ( 'Il y a 0 pomme' , 'one: There is one apple|more: There is %count% apples' , 'one: Il y a %count% pomme|more: Il y a %count% pommes' , 0 , array ( '%count%' => 0 ), 'fr' , '' ),
array ( 'Il y a 1 pomme' , 'one: There is one apple|more: There is %count% apples' , 'one: Il y a %count% pomme|more: Il y a %count% pommes' , 1 , array ( '%count%' => 1 ), 'fr' , '' ),
array ( 'Il y a 10 pommes' , 'one: There is one apple|more: There is %count% apples' , 'one: Il y a %count% pomme|more: Il y a %count% pommes' , 10 , array ( '%count%' => 10 ), 'fr' , '' ),
array ( 'Il n\'y a aucune pomme' , '{0} There is no apple|one: There is one apple|more: There is %count% apples' , '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes' , 0 , array ( '%count%' => 0 ), 'fr' , '' ),
array ( 'Il y a 1 pomme' , '{0} There is no apple|one: There is one apple|more: There is %count% apples' , '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes' , 1 , array ( '%count%' => 1 ), 'fr' , '' ),
array ( 'Il y a 10 pommes' , '{0} There is no apple|one: There is one apple|more: There is %count% apples' , '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes' , 10 , array ( '%count%' => 10 ), 'fr' , '' ),
2011-04-24 08:39:28 +01:00
array ( 'Il y a 0 pomme' , new String ( '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples' ), '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes' , 0 , array ( '%count%' => 0 ), 'fr' , '' ),
2010-09-27 08:45:29 +01:00
);
}
2011-08-22 15:07:39 +01:00
public function testTransChoiceFallback ()
{
$translator = new Translator ( 'ru' , new MessageSelector ());
$translator -> setFallbackLocale ( 'en' );
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'some_message2' => 'one thing|%count% things' ), 'en' );
$this -> assertEquals ( '10 things' , $translator -> transChoice ( 'some_message2' , 10 , array ( '%count%' => 10 )));
}
2011-09-15 07:19:52 +01:00
public function testTransChoiceFallbackBis ()
{
$translator = new Translator ( 'ru' , new MessageSelector ());
$translator -> setFallbackLocale ( array ( 'en_US' , 'en' ));
$translator -> addLoader ( 'array' , new ArrayLoader ());
$translator -> addResource ( 'array' , array ( 'some_message2' => 'one thing|%count% things' ), 'en_US' );
$this -> assertEquals ( '10 things' , $translator -> transChoice ( 'some_message2' , 10 , array ( '%count%' => 10 )));
}
/**
* @ expectedException \InvalidArgumentException
*/
public function testTransChoiceFallbackWithNoTranslation ()
{
$translator = new Translator ( 'ru' , new MessageSelector ());
$translator -> setFallbackLocale ( 'en' );
$translator -> addLoader ( 'array' , new ArrayLoader ());
$this -> assertEquals ( '10 things' , $translator -> transChoice ( 'some_message2' , 10 , array ( '%count%' => 10 )));
}
2010-09-27 08:45:29 +01:00
}
2011-04-24 08:39:28 +01:00
class String
{
protected $str ;
public function __construct ( $str )
{
$this -> str = $str ;
}
public function __toString ()
{
return $this -> str ;
}
}