123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Translation\Tests;
- use Symfony\Component\Translation\Translator;
- use Symfony\Component\Translation\MessageSelector;
- use Symfony\Component\Translation\Loader\ArrayLoader;
- class TranslatorTest extends \PHPUnit_Framework_TestCase
- {
- public function testSetGetLocale()
- {
- $translator = new Translator('en', new MessageSelector());
- $this->assertEquals('en', $translator->getLocale());
- $translator->setLocale('fr');
- $this->assertEquals('fr', $translator->getLocale());
- }
- public function testSetFallbackLocales()
- {
- $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->setFallbackLocales(array('fr'));
- $this->assertEquals('foobar', $translator->trans('bar'));
- }
- public function testSetFallbackLocalesMultiple()
- {
- $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->setFallbackLocales(array('fr_FR', 'fr'));
- $this->assertEquals('bar (fr)', $translator->trans('bar'));
- }
- public function testTransWithFallbackLocale()
- {
- $translator = new Translator('fr_FR', new MessageSelector());
- $translator->addLoader('array', new ArrayLoader());
- $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
- $translator->addResource('array', array('bar' => 'foobar'), 'en');
- $translator->setFallbackLocales(array('en'));
- $this->assertEquals('foobar', $translator->trans('bar'));
- }
- public function testAddResourceAfterTrans()
- {
- $translator = new Translator('fr', new MessageSelector());
- $translator->addLoader('array', new ArrayLoader());
- $translator->setFallbackLocale(array('en'));
- $translator->addResource('array', array('foo' => 'foofoo'), 'en');
- $this->assertEquals('foofoo', $translator->trans('foo'));
- $translator->addResource('array', array('bar' => 'foobar'), 'en');
- $this->assertEquals('foobar', $translator->trans('bar'));
- }
- /**
- * @dataProvider getTransFileTests
- * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
- */
- public function testTransWithoutFallbackLocaleFile($format, $loader)
- {
- $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
- $translator = new Translator('en', new MessageSelector());
- $translator->addLoader($format, new $loaderClass());
- $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en');
- $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en');
- // force catalogue loading
- $translator->trans('foo');
- }
- /**
- * @dataProvider getTransFileTests
- */
- public function testTransWithFallbackLocaleFile($format, $loader)
- {
- $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
- $translator = new Translator('en_GB', new MessageSelector());
- $translator->addLoader($format, new $loaderClass());
- $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB');
- $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources');
- $this->assertEquals('bar', $translator->trans('foo', array(), 'resources'));
- }
- 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'));
- }
- 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->setFallbackLocales(array('en_US', 'en'));
- $this->assertEquals('foo (en_US)', $translator->trans('foo'));
- $this->assertEquals('bar (en)', $translator->trans('bar'));
- }
- public function testTransNonExistentWithFallback()
- {
- $translator = new Translator('fr', new MessageSelector());
- $translator->setFallbackLocales(array('en'));
- $translator->addLoader('array', new ArrayLoader());
- $this->assertEquals('non-existent', $translator->trans('non-existent'));
- }
- /**
- * @expectedException RuntimeException
- */
- public function testWhenAResourceHasNoRegisteredLoader()
- {
- $translator = new Translator('en', new MessageSelector());
- $translator->addResource('array', array('foo' => 'foofoo'), 'en');
- $translator->trans('foo');
- }
- /**
- * @dataProvider getTransTests
- */
- public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
- {
- $translator = new Translator('en', new MessageSelector());
- $translator->addLoader('array', new ArrayLoader());
- $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
- $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
- }
- /**
- * @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'));
- }
- /**
- * @dataProvider getTransChoiceTests
- */
- public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
- {
- $translator = new Translator('en', new MessageSelector());
- $translator->addLoader('array', new ArrayLoader());
- $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
- $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
- }
- public function getTransFileTests()
- {
- return array(
- array('csv', 'CsvFileLoader'),
- array('ini', 'IniFileLoader'),
- array('mo', 'MoFileLoader'),
- array('po', 'PoFileLoader'),
- array('php', 'PhpFileLoader'),
- array('ts', 'QtFileLoader'),
- array('xlf', 'XliffFileLoader'),
- array('yml', 'YamlFileLoader'),
- );
- }
- 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', ''),
- array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''),
- );
- }
- 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'),
- );
- }
- public function getTransChoiceTests()
- {
- return array(
- array('Il y a 0 pomme', '{0} There are no appless|{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 are no appless|{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 are no appless|{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 are no apples|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 are no apples|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 are no apples|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', ''),
- array('Il y a 0 pomme', new String('{0} There are no appless|{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', ''),
- );
- }
- public function testTransChoiceFallback()
- {
- $translator = new Translator('ru', new MessageSelector());
- $translator->setFallbackLocales(array('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)));
- }
- public function testTransChoiceFallbackBis()
- {
- $translator = new Translator('ru', new MessageSelector());
- $translator->setFallbackLocales(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)));
- }
- public function testTransChoiceFallbackWithNoTranslation()
- {
- $translator = new Translator('ru', new MessageSelector());
- $translator->setFallbackLocales(array('en'));
- $translator->addLoader('array', new ArrayLoader());
- // consistent behavior with Translator::trans(), which returns the string
- // unchanged if it can't be found
- $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
- }
- }
- class String
- {
- protected $str;
- public function __construct($str)
- {
- $this->str = $str;
- }
- public function __toString()
- {
- return $this->str;
- }
- }
|