TranslatorTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Tests;
  11. use Symfony\Component\Translation\Translator;
  12. use Symfony\Component\Translation\MessageSelector;
  13. use Symfony\Component\Translation\Loader\ArrayLoader;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. class TranslatorTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @dataProvider getInvalidLocalesTests
  19. * @expectedException \InvalidArgumentException
  20. */
  21. public function testConstructorInvalidLocale($locale)
  22. {
  23. $translator = new Translator($locale, new MessageSelector());
  24. }
  25. /**
  26. * @dataProvider getValidLocalesTests
  27. */
  28. public function testConstructorValidLocale($locale)
  29. {
  30. $translator = new Translator($locale, new MessageSelector());
  31. $this->assertEquals($locale, $translator->getLocale());
  32. }
  33. public function testConstructorWithoutLocale()
  34. {
  35. $translator = new Translator(null, new MessageSelector());
  36. $this->assertNull($translator->getLocale());
  37. }
  38. public function testSetGetLocale()
  39. {
  40. $translator = new Translator('en');
  41. $this->assertEquals('en', $translator->getLocale());
  42. $translator->setLocale('fr');
  43. $this->assertEquals('fr', $translator->getLocale());
  44. }
  45. /**
  46. * @dataProvider getInvalidLocalesTests
  47. * @expectedException \InvalidArgumentException
  48. */
  49. public function testSetInvalidLocale($locale)
  50. {
  51. $translator = new Translator('fr', new MessageSelector());
  52. $translator->setLocale($locale);
  53. }
  54. /**
  55. * @dataProvider getValidLocalesTests
  56. */
  57. public function testSetValidLocale($locale)
  58. {
  59. $translator = new Translator($locale, new MessageSelector());
  60. $translator->setLocale($locale);
  61. $this->assertEquals($locale, $translator->getLocale());
  62. }
  63. public function testGetCatalogue()
  64. {
  65. $translator = new Translator('en');
  66. $this->assertEquals(new MessageCatalogue('en'), $translator->getCatalogue());
  67. $translator->setLocale('fr');
  68. $this->assertEquals(new MessageCatalogue('fr'), $translator->getCatalogue('fr'));
  69. }
  70. public function testGetCatalogueReturnsConsolidatedCatalogue()
  71. {
  72. /*
  73. * This will be useful once we refactor so that different domains will be loaded lazily (on-demand).
  74. * In that case, getCatalogue() will probably have to load all missing domains in order to return
  75. * one complete catalogue.
  76. */
  77. $locale = 'whatever';
  78. $translator = new Translator($locale);
  79. $translator->addLoader('loader-a', new ArrayLoader());
  80. $translator->addLoader('loader-b', new ArrayLoader());
  81. $translator->addResource('loader-a', array('foo' => 'foofoo'), $locale, 'domain-a');
  82. $translator->addResource('loader-b', array('bar' => 'foobar'), $locale, 'domain-b');
  83. /*
  84. * Test that we get a single catalogue comprising messages
  85. * from different loaders and different domains
  86. */
  87. $catalogue = $translator->getCatalogue($locale);
  88. $this->assertTrue($catalogue->defines('foo', 'domain-a'));
  89. $this->assertTrue($catalogue->defines('bar', 'domain-b'));
  90. }
  91. public function testSetFallbackLocales()
  92. {
  93. $translator = new Translator('en');
  94. $translator->addLoader('array', new ArrayLoader());
  95. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  96. $translator->addResource('array', array('bar' => 'foobar'), 'fr');
  97. // force catalogue loading
  98. $translator->trans('bar');
  99. $translator->setFallbackLocales(array('fr'));
  100. $this->assertEquals('foobar', $translator->trans('bar'));
  101. }
  102. public function testSetFallbackLocalesMultiple()
  103. {
  104. $translator = new Translator('en');
  105. $translator->addLoader('array', new ArrayLoader());
  106. $translator->addResource('array', array('foo' => 'foo (en)'), 'en');
  107. $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
  108. // force catalogue loading
  109. $translator->trans('bar');
  110. $translator->setFallbackLocales(array('fr_FR', 'fr'));
  111. $this->assertEquals('bar (fr)', $translator->trans('bar'));
  112. }
  113. /**
  114. * @dataProvider getInvalidLocalesTests
  115. * @expectedException \InvalidArgumentException
  116. */
  117. public function testSetFallbackInvalidLocales($locale)
  118. {
  119. $translator = new Translator('fr', new MessageSelector());
  120. $translator->setFallbackLocales(array('fr', $locale));
  121. }
  122. /**
  123. * @dataProvider getValidLocalesTests
  124. */
  125. public function testSetFallbackValidLocales($locale)
  126. {
  127. $translator = new Translator($locale, new MessageSelector());
  128. $translator->setFallbackLocales(array('fr', $locale));
  129. // no assertion. this method just asserts that no exception is thrown
  130. }
  131. public function testTransWithFallbackLocale()
  132. {
  133. $translator = new Translator('fr_FR');
  134. $translator->setFallbackLocales(array('en'));
  135. $translator->addLoader('array', new ArrayLoader());
  136. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  137. $this->assertEquals('foobar', $translator->trans('bar'));
  138. }
  139. /**
  140. * @dataProvider getInvalidLocalesTests
  141. * @expectedException \InvalidArgumentException
  142. */
  143. public function testAddResourceInvalidLocales($locale)
  144. {
  145. $translator = new Translator('fr', new MessageSelector());
  146. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  147. }
  148. /**
  149. * @dataProvider getValidLocalesTests
  150. */
  151. public function testAddResourceValidLocales($locale)
  152. {
  153. $translator = new Translator('fr', new MessageSelector());
  154. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  155. // no assertion. this method just asserts that no exception is thrown
  156. }
  157. public function testAddResourceAfterTrans()
  158. {
  159. $translator = new Translator('fr');
  160. $translator->addLoader('array', new ArrayLoader());
  161. $translator->setFallbackLocales(array('en'));
  162. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  163. $this->assertEquals('foofoo', $translator->trans('foo'));
  164. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  165. $this->assertEquals('foobar', $translator->trans('bar'));
  166. }
  167. /**
  168. * @dataProvider getTransFileTests
  169. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  170. */
  171. public function testTransWithoutFallbackLocaleFile($format, $loader)
  172. {
  173. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  174. $translator = new Translator('en');
  175. $translator->addLoader($format, new $loaderClass());
  176. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en');
  177. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en');
  178. // force catalogue loading
  179. $translator->trans('foo');
  180. }
  181. /**
  182. * @dataProvider getTransFileTests
  183. */
  184. public function testTransWithFallbackLocaleFile($format, $loader)
  185. {
  186. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  187. $translator = new Translator('en_GB');
  188. $translator->addLoader($format, new $loaderClass());
  189. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB');
  190. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources');
  191. $this->assertEquals('bar', $translator->trans('foo', array(), 'resources'));
  192. }
  193. public function testTransWithFallbackLocaleBis()
  194. {
  195. $translator = new Translator('en_US');
  196. $translator->addLoader('array', new ArrayLoader());
  197. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  198. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  199. $this->assertEquals('foobar', $translator->trans('bar'));
  200. }
  201. public function testTransWithFallbackLocaleTer()
  202. {
  203. $translator = new Translator('fr_FR');
  204. $translator->addLoader('array', new ArrayLoader());
  205. $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
  206. $translator->addResource('array', array('bar' => 'bar (en)'), 'en');
  207. $translator->setFallbackLocales(array('en_US', 'en'));
  208. $this->assertEquals('foo (en_US)', $translator->trans('foo'));
  209. $this->assertEquals('bar (en)', $translator->trans('bar'));
  210. }
  211. public function testTransNonExistentWithFallback()
  212. {
  213. $translator = new Translator('fr');
  214. $translator->setFallbackLocales(array('en'));
  215. $translator->addLoader('array', new ArrayLoader());
  216. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  217. }
  218. /**
  219. * @expectedException \RuntimeException
  220. */
  221. public function testWhenAResourceHasNoRegisteredLoader()
  222. {
  223. $translator = new Translator('en');
  224. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  225. $translator->trans('foo');
  226. }
  227. public function testFallbackCatalogueResources()
  228. {
  229. $translator = new Translator('en_GB', new MessageSelector());
  230. $translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
  231. $translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
  232. $translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');
  233. // force catalogue loading
  234. $this->assertEquals('bar', $translator->trans('foo', array()));
  235. $resources = $translator->getCatalogue('en')->getResources();
  236. $this->assertCount(1, $resources);
  237. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources);
  238. $resources = $translator->getCatalogue('en_GB')->getResources();
  239. $this->assertCount(2, $resources);
  240. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'empty.yml', $resources);
  241. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources);
  242. }
  243. /**
  244. * @dataProvider getTransTests
  245. */
  246. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  247. {
  248. $translator = new Translator('en');
  249. $translator->addLoader('array', new ArrayLoader());
  250. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  251. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  252. }
  253. /**
  254. * @dataProvider getInvalidLocalesTests
  255. * @expectedException \InvalidArgumentException
  256. */
  257. public function testTransInvalidLocale($locale)
  258. {
  259. $translator = new Translator('en', new MessageSelector());
  260. $translator->addLoader('array', new ArrayLoader());
  261. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  262. $translator->trans('foo', array(), '', $locale);
  263. }
  264. /**
  265. * @dataProvider getValidLocalesTests
  266. */
  267. public function testTransValidLocale($locale)
  268. {
  269. $translator = new Translator($locale, new MessageSelector());
  270. $translator->addLoader('array', new ArrayLoader());
  271. $translator->addResource('array', array('test' => 'OK'), $locale);
  272. $this->assertEquals('OK', $translator->trans('test'));
  273. $this->assertEquals('OK', $translator->trans('test', array(), null, $locale));
  274. }
  275. /**
  276. * @dataProvider getFlattenedTransTests
  277. */
  278. public function testFlattenedTrans($expected, $messages, $id)
  279. {
  280. $translator = new Translator('en');
  281. $translator->addLoader('array', new ArrayLoader());
  282. $translator->addResource('array', $messages, 'fr', '');
  283. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  284. }
  285. /**
  286. * @dataProvider getTransChoiceTests
  287. */
  288. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  289. {
  290. $translator = new Translator('en');
  291. $translator->addLoader('array', new ArrayLoader());
  292. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  293. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  294. }
  295. /**
  296. * @dataProvider getInvalidLocalesTests
  297. * @expectedException \InvalidArgumentException
  298. */
  299. public function testTransChoiceInvalidLocale($locale)
  300. {
  301. $translator = new Translator('en', new MessageSelector());
  302. $translator->addLoader('array', new ArrayLoader());
  303. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  304. $translator->transChoice('foo', 1, array(), '', $locale);
  305. }
  306. /**
  307. * @dataProvider getValidLocalesTests
  308. */
  309. public function testTransChoiceValidLocale($locale)
  310. {
  311. $translator = new Translator('en', new MessageSelector());
  312. $translator->addLoader('array', new ArrayLoader());
  313. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  314. $translator->transChoice('foo', 1, array(), '', $locale);
  315. // no assertion. this method just asserts that no exception is thrown
  316. }
  317. public function getTransFileTests()
  318. {
  319. return array(
  320. array('csv', 'CsvFileLoader'),
  321. array('ini', 'IniFileLoader'),
  322. array('mo', 'MoFileLoader'),
  323. array('po', 'PoFileLoader'),
  324. array('php', 'PhpFileLoader'),
  325. array('ts', 'QtFileLoader'),
  326. array('xlf', 'XliffFileLoader'),
  327. array('yml', 'YamlFileLoader'),
  328. array('json', 'JsonFileLoader'),
  329. );
  330. }
  331. public function getTransTests()
  332. {
  333. return array(
  334. array('Symfony est super !', 'Symfony is great!', 'Symfony est super !', array(), 'fr', ''),
  335. array('Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  336. array('Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', array(), 'fr', ''),
  337. );
  338. }
  339. public function getFlattenedTransTests()
  340. {
  341. $messages = array(
  342. 'symfony' => array(
  343. 'is' => array(
  344. 'great' => 'Symfony est super!',
  345. ),
  346. ),
  347. 'foo' => array(
  348. 'bar' => array(
  349. 'baz' => 'Foo Bar Baz',
  350. ),
  351. 'baz' => 'Foo Baz',
  352. ),
  353. );
  354. return array(
  355. array('Symfony est super!', $messages, 'symfony.is.great'),
  356. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  357. array('Foo Baz', $messages, 'foo.baz'),
  358. );
  359. }
  360. public function getTransChoiceTests()
  361. {
  362. return array(
  363. 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', ''),
  364. 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', ''),
  365. 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', ''),
  366. 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', ''),
  367. 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', ''),
  368. 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', ''),
  369. 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', ''),
  370. 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', ''),
  371. 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', ''),
  372. 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', ''),
  373. 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', ''),
  374. 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', ''),
  375. array('Il y a 0 pomme', new StringClass('{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', ''),
  376. );
  377. }
  378. public function getInvalidLocalesTests()
  379. {
  380. return array(
  381. array('fr FR'),
  382. array('français'),
  383. array('fr+en'),
  384. array('utf#8'),
  385. array('fr&en'),
  386. array('fr~FR'),
  387. array(' fr'),
  388. array('fr '),
  389. array('fr*'),
  390. array('fr/FR'),
  391. array('fr\\FR'),
  392. );
  393. }
  394. public function getValidLocalesTests()
  395. {
  396. return array(
  397. array(''),
  398. array(null),
  399. array('fr'),
  400. array('francais'),
  401. array('FR'),
  402. array('frFR'),
  403. array('fr-FR'),
  404. array('fr_FR'),
  405. array('fr.FR'),
  406. array('fr-FR.UTF8'),
  407. array('sr@latin'),
  408. );
  409. }
  410. public function testTransChoiceFallback()
  411. {
  412. $translator = new Translator('ru');
  413. $translator->setFallbackLocales(array('en'));
  414. $translator->addLoader('array', new ArrayLoader());
  415. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
  416. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  417. }
  418. public function testTransChoiceFallbackBis()
  419. {
  420. $translator = new Translator('ru');
  421. $translator->setFallbackLocales(array('en_US', 'en'));
  422. $translator->addLoader('array', new ArrayLoader());
  423. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
  424. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  425. }
  426. public function testTransChoiceFallbackWithNoTranslation()
  427. {
  428. $translator = new Translator('ru');
  429. $translator->setFallbackLocales(array('en'));
  430. $translator->addLoader('array', new ArrayLoader());
  431. // consistent behavior with Translator::trans(), which returns the string
  432. // unchanged if it can't be found
  433. $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  434. }
  435. }
  436. class StringClass
  437. {
  438. protected $str;
  439. public function __construct($str)
  440. {
  441. $this->str = $str;
  442. }
  443. public function __toString()
  444. {
  445. return $this->str;
  446. }
  447. }