4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests
;
14 use Symfony\Component\Translation\Translator
;
15 use Symfony\Component\Translation\MessageSelector
;
16 use Symfony\Component\Translation\Loader\ArrayLoader
;
18 class TranslatorTest
extends \PHPUnit_Framework_TestCase
20 public function testSetGetLocale()
22 $translator = new Translator('en', new MessageSelector());
24 $this->assertEquals('en', $translator->getLocale());
26 $translator->setLocale('fr');
27 $this->assertEquals('fr', $translator->getLocale());
30 public function testSetFallbackLocales()
32 $translator = new Translator('en', new MessageSelector());
33 $translator->addLoader('array', new ArrayLoader());
34 $translator->addResource('array', array('foo' => 'foofoo'), 'en');
35 $translator->addResource('array', array('bar' => 'foobar'), 'fr');
37 // force catalogue loading
38 $translator->trans('bar');
40 $translator->setFallbackLocales(array('fr'));
41 $this->assertEquals('foobar', $translator->trans('bar'));
44 public function testSetFallbackLocalesMultiple()
46 $translator = new Translator('en', new MessageSelector());
47 $translator->addLoader('array', new ArrayLoader());
48 $translator->addResource('array', array('foo' => 'foo (en)'), 'en');
49 $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
51 // force catalogue loading
52 $translator->trans('bar');
54 $translator->setFallbackLocales(array('fr_FR', 'fr'));
55 $this->assertEquals('bar (fr)', $translator->trans('bar'));
58 public function testTransWithFallbackLocale()
60 $translator = new Translator('fr_FR', new MessageSelector());
61 $translator->addLoader('array', new ArrayLoader());
62 $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
63 $translator->addResource('array', array('bar' => 'foobar'), 'en');
65 $translator->setFallbackLocales(array('en'));
67 $this->assertEquals('foobar', $translator->trans('bar'));
70 public function testAddResourceAfterTrans()
72 $translator = new Translator('fr', new MessageSelector());
73 $translator->addLoader('array', new ArrayLoader());
75 $translator->setFallbackLocale(array('en'));
77 $translator->addResource('array', array('foo' => 'foofoo'), 'en');
78 $this->assertEquals('foofoo', $translator->trans('foo'));
80 $translator->addResource('array', array('bar' => 'foobar'), 'en');
81 $this->assertEquals('foobar', $translator->trans('bar'));
85 * @dataProvider getTransFileTests
86 * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
88 public function testTransWithoutFallbackLocaleFile($format, $loader)
90 $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
91 $translator = new Translator('en', new MessageSelector());
92 $translator->addLoader($format, new $loaderClass());
93 $translator->addResource($format, __DIR__
.'/fixtures/non-existing', 'en');
94 $translator->addResource($format, __DIR__
.'/fixtures/resources.'.$format, 'en');
96 // force catalogue loading
97 $translator->trans('foo');
101 * @dataProvider getTransFileTests
103 public function testTransWithFallbackLocaleFile($format, $loader)
105 $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
106 $translator = new Translator('en_GB', new MessageSelector());
107 $translator->addLoader($format, new $loaderClass());
108 $translator->addResource($format, __DIR__
.'/fixtures/non-existing', 'en_GB');
109 $translator->addResource($format, __DIR__
.'/fixtures/resources.'.$format, 'en', 'resources');
111 $this->assertEquals('bar', $translator->trans('foo', array(), 'resources'));
114 public function testTransWithFallbackLocaleBis()
116 $translator = new Translator('en_US', new MessageSelector());
117 $translator->addLoader('array', new ArrayLoader());
118 $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
119 $translator->addResource('array', array('bar' => 'foobar'), 'en');
120 $this->assertEquals('foobar', $translator->trans('bar'));
123 public function testTransWithFallbackLocaleTer()
125 $translator = new Translator('fr_FR', new MessageSelector());
126 $translator->addLoader('array', new ArrayLoader());
127 $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
128 $translator->addResource('array', array('bar' => 'bar (en)'), 'en');
130 $translator->setFallbackLocales(array('en_US', 'en'));
132 $this->assertEquals('foo (en_US)', $translator->trans('foo'));
133 $this->assertEquals('bar (en)', $translator->trans('bar'));
136 public function testTransNonExistentWithFallback()
138 $translator = new Translator('fr', new MessageSelector());
139 $translator->setFallbackLocales(array('en'));
140 $translator->addLoader('array', new ArrayLoader());
141 $this->assertEquals('non-existent', $translator->trans('non-existent'));
145 * @expectedException RuntimeException
147 public function testWhenAResourceHasNoRegisteredLoader()
149 $translator = new Translator('en', new MessageSelector());
150 $translator->addResource('array', array('foo' => 'foofoo'), 'en');
152 $translator->trans('foo');
156 * @dataProvider getTransTests
158 public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
160 $translator = new Translator('en', new MessageSelector());
161 $translator->addLoader('array', new ArrayLoader());
162 $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
164 $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
168 * @dataProvider getFlattenedTransTests
170 public function testFlattenedTrans($expected, $messages, $id)
172 $translator = new Translator('en', new MessageSelector());
173 $translator->addLoader('array', new ArrayLoader());
174 $translator->addResource('array', $messages, 'fr', '');
176 $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
180 * @dataProvider getTransChoiceTests
182 public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
184 $translator = new Translator('en', new MessageSelector());
185 $translator->addLoader('array', new ArrayLoader());
186 $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
188 $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
191 public function getTransFileTests()
194 array('csv', 'CsvFileLoader'),
195 array('ini', 'IniFileLoader'),
196 array('mo', 'MoFileLoader'),
197 array('po', 'PoFileLoader'),
198 array('php', 'PhpFileLoader'),
199 array('ts', 'QtFileLoader'),
200 array('xlf', 'XliffFileLoader'),
201 array('yml', 'YamlFileLoader'),
205 public function getTransTests()
208 array('Symfony2 est super !', 'Symfony2 is great!', 'Symfony2 est super !', array(), 'fr', ''),
209 array('Symfony2 est awesome !', 'Symfony2 is %what%!', 'Symfony2 est %what% !', array('%what%' => 'awesome'), 'fr', ''),
210 array('Symfony2 est super !', new String('Symfony2 is great!'), 'Symfony2 est super !', array(), 'fr', ''),
214 public function getFlattenedTransTests()
219 'great' => 'Symfony2 est super!'
224 'baz' => 'Foo Bar Baz'
231 array('Symfony2 est super!', $messages, 'symfony2.is.great'),
232 array('Foo Bar Baz', $messages, 'foo.bar.baz'),
233 array('Foo Baz', $messages, 'foo.baz'),
237 public function getTransChoiceTests()
240 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', ''),
241 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', ''),
242 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', ''),
244 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', ''),
245 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', ''),
246 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', ''),
248 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', ''),
249 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', ''),
250 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', ''),
252 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', ''),
253 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', ''),
254 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', ''),
256 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', ''),
260 public function testTransChoiceFallback()
262 $translator = new Translator('ru', new MessageSelector());
263 $translator->setFallbackLocales(array('en'));
264 $translator->addLoader('array', new ArrayLoader());
265 $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
267 $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
270 public function testTransChoiceFallbackBis()
272 $translator = new Translator('ru', new MessageSelector());
273 $translator->setFallbackLocales(array('en_US', 'en'));
274 $translator->addLoader('array', new ArrayLoader());
275 $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
277 $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
281 * @expectedException \InvalidArgumentException
283 public function testTransChoiceFallbackWithNoTranslation()
285 $translator = new Translator('ru', new MessageSelector());
286 $translator->setFallbackLocales(array('en'));
287 $translator->addLoader('array', new ArrayLoader());
289 $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
297 public function __construct($str)
302 public function __toString()