TranslatorTest.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\Translator;
  13. use Symfony\Component\Translation\Loader\ArrayLoader;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. class TranslatorTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider getInvalidLocalesTests
  19. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  20. */
  21. public function testConstructorInvalidLocale($locale)
  22. {
  23. new Translator($locale);
  24. }
  25. /**
  26. * @dataProvider getValidLocalesTests
  27. */
  28. public function testConstructorValidLocale($locale)
  29. {
  30. $translator = new Translator($locale);
  31. $this->assertEquals($locale, $translator->getLocale());
  32. }
  33. public function testConstructorWithoutLocale()
  34. {
  35. $translator = new Translator(null);
  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 \Symfony\Component\Translation\Exception\InvalidArgumentException
  48. */
  49. public function testSetInvalidLocale($locale)
  50. {
  51. $translator = new Translator('fr');
  52. $translator->setLocale($locale);
  53. }
  54. /**
  55. * @dataProvider getValidLocalesTests
  56. */
  57. public function testSetValidLocale($locale)
  58. {
  59. $translator = new Translator($locale);
  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 \Symfony\Component\Translation\Exception\InvalidArgumentException
  116. */
  117. public function testSetFallbackInvalidLocales($locale)
  118. {
  119. $translator = new Translator('fr');
  120. $translator->setFallbackLocales(array('fr', $locale));
  121. }
  122. /**
  123. * @dataProvider getValidLocalesTests
  124. */
  125. public function testSetFallbackValidLocales($locale)
  126. {
  127. $translator = new Translator($locale);
  128. $translator->setFallbackLocales(array('fr', $locale));
  129. // no assertion. this method just asserts that no exception is thrown
  130. $this->addToAssertionCount(1);
  131. }
  132. public function testTransWithFallbackLocale()
  133. {
  134. $translator = new Translator('fr_FR');
  135. $translator->setFallbackLocales(array('en'));
  136. $translator->addLoader('array', new ArrayLoader());
  137. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  138. $this->assertEquals('foobar', $translator->trans('bar'));
  139. }
  140. /**
  141. * @dataProvider getInvalidLocalesTests
  142. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  143. */
  144. public function testAddResourceInvalidLocales($locale)
  145. {
  146. $translator = new Translator('fr');
  147. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  148. }
  149. /**
  150. * @dataProvider getValidLocalesTests
  151. */
  152. public function testAddResourceValidLocales($locale)
  153. {
  154. $translator = new Translator('fr');
  155. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  156. // no assertion. this method just asserts that no exception is thrown
  157. $this->addToAssertionCount(1);
  158. }
  159. public function testAddResourceAfterTrans()
  160. {
  161. $translator = new Translator('fr');
  162. $translator->addLoader('array', new ArrayLoader());
  163. $translator->setFallbackLocales(array('en'));
  164. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  165. $this->assertEquals('foofoo', $translator->trans('foo'));
  166. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  167. $this->assertEquals('foobar', $translator->trans('bar'));
  168. }
  169. /**
  170. * @dataProvider getTransFileTests
  171. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  172. */
  173. public function testTransWithoutFallbackLocaleFile($format, $loader)
  174. {
  175. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  176. $translator = new Translator('en');
  177. $translator->addLoader($format, new $loaderClass());
  178. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en');
  179. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en');
  180. // force catalogue loading
  181. $translator->trans('foo');
  182. }
  183. /**
  184. * @dataProvider getTransFileTests
  185. */
  186. public function testTransWithFallbackLocaleFile($format, $loader)
  187. {
  188. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  189. $translator = new Translator('en_GB');
  190. $translator->addLoader($format, new $loaderClass());
  191. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB');
  192. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources');
  193. $this->assertEquals('bar', $translator->trans('foo', array(), 'resources'));
  194. }
  195. public function testTransWithFallbackLocaleBis()
  196. {
  197. $translator = new Translator('en_US');
  198. $translator->addLoader('array', new ArrayLoader());
  199. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  200. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  201. $this->assertEquals('foobar', $translator->trans('bar'));
  202. }
  203. public function testTransWithFallbackLocaleTer()
  204. {
  205. $translator = new Translator('fr_FR');
  206. $translator->addLoader('array', new ArrayLoader());
  207. $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
  208. $translator->addResource('array', array('bar' => 'bar (en)'), 'en');
  209. $translator->setFallbackLocales(array('en_US', 'en'));
  210. $this->assertEquals('foo (en_US)', $translator->trans('foo'));
  211. $this->assertEquals('bar (en)', $translator->trans('bar'));
  212. }
  213. public function testTransNonExistentWithFallback()
  214. {
  215. $translator = new Translator('fr');
  216. $translator->setFallbackLocales(array('en'));
  217. $translator->addLoader('array', new ArrayLoader());
  218. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  219. }
  220. /**
  221. * @expectedException \Symfony\Component\Translation\Exception\RuntimeException
  222. */
  223. public function testWhenAResourceHasNoRegisteredLoader()
  224. {
  225. $translator = new Translator('en');
  226. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  227. $translator->trans('foo');
  228. }
  229. public function testNestedFallbackCatalogueWhenUsingMultipleLocales()
  230. {
  231. $translator = new Translator('fr');
  232. $translator->setFallbackLocales(array('ru', 'en'));
  233. $translator->getCatalogue('fr');
  234. $this->assertNotNull($translator->getCatalogue('ru')->getFallbackCatalogue());
  235. }
  236. public function testFallbackCatalogueResources()
  237. {
  238. $translator = new Translator('en_GB');
  239. $translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
  240. $translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
  241. $translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');
  242. // force catalogue loading
  243. $this->assertEquals('bar', $translator->trans('foo', array()));
  244. $resources = $translator->getCatalogue('en')->getResources();
  245. $this->assertCount(1, $resources);
  246. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources);
  247. $resources = $translator->getCatalogue('en_GB')->getResources();
  248. $this->assertCount(2, $resources);
  249. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'empty.yml', $resources);
  250. $this->assertContains(__DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'resources.yml', $resources);
  251. }
  252. /**
  253. * @dataProvider getTransTests
  254. */
  255. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  256. {
  257. $translator = new Translator('en');
  258. $translator->addLoader('array', new ArrayLoader());
  259. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  260. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  261. }
  262. /**
  263. * @dataProvider getInvalidLocalesTests
  264. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  265. */
  266. public function testTransInvalidLocale($locale)
  267. {
  268. $translator = new Translator('en');
  269. $translator->addLoader('array', new ArrayLoader());
  270. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  271. $translator->trans('foo', array(), '', $locale);
  272. }
  273. /**
  274. * @dataProvider getValidLocalesTests
  275. */
  276. public function testTransValidLocale($locale)
  277. {
  278. $translator = new Translator($locale);
  279. $translator->addLoader('array', new ArrayLoader());
  280. $translator->addResource('array', array('test' => 'OK'), $locale);
  281. $this->assertEquals('OK', $translator->trans('test'));
  282. $this->assertEquals('OK', $translator->trans('test', array(), null, $locale));
  283. }
  284. /**
  285. * @dataProvider getFlattenedTransTests
  286. */
  287. public function testFlattenedTrans($expected, $messages, $id)
  288. {
  289. $translator = new Translator('en');
  290. $translator->addLoader('array', new ArrayLoader());
  291. $translator->addResource('array', $messages, 'fr', '');
  292. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  293. }
  294. /**
  295. * @dataProvider getTransChoiceTests
  296. */
  297. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  298. {
  299. $translator = new Translator('en');
  300. $translator->addLoader('array', new ArrayLoader());
  301. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  302. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  303. }
  304. /**
  305. * @dataProvider getInvalidLocalesTests
  306. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  307. */
  308. public function testTransChoiceInvalidLocale($locale)
  309. {
  310. $translator = new Translator('en');
  311. $translator->addLoader('array', new ArrayLoader());
  312. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  313. $translator->transChoice('foo', 1, array(), '', $locale);
  314. }
  315. /**
  316. * @dataProvider getValidLocalesTests
  317. */
  318. public function testTransChoiceValidLocale($locale)
  319. {
  320. $translator = new Translator('en');
  321. $translator->addLoader('array', new ArrayLoader());
  322. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  323. $translator->transChoice('foo', 1, array(), '', $locale);
  324. // no assertion. this method just asserts that no exception is thrown
  325. $this->addToAssertionCount(1);
  326. }
  327. public function getTransFileTests()
  328. {
  329. return array(
  330. array('csv', 'CsvFileLoader'),
  331. array('ini', 'IniFileLoader'),
  332. array('mo', 'MoFileLoader'),
  333. array('po', 'PoFileLoader'),
  334. array('php', 'PhpFileLoader'),
  335. array('ts', 'QtFileLoader'),
  336. array('xlf', 'XliffFileLoader'),
  337. array('yml', 'YamlFileLoader'),
  338. array('json', 'JsonFileLoader'),
  339. );
  340. }
  341. public function getTransTests()
  342. {
  343. return array(
  344. array('Symfony est super !', 'Symfony is great!', 'Symfony est super !', array(), 'fr', ''),
  345. array('Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  346. array('Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', array(), 'fr', ''),
  347. );
  348. }
  349. public function getFlattenedTransTests()
  350. {
  351. $messages = array(
  352. 'symfony' => array(
  353. 'is' => array(
  354. 'great' => 'Symfony est super!',
  355. ),
  356. ),
  357. 'foo' => array(
  358. 'bar' => array(
  359. 'baz' => 'Foo Bar Baz',
  360. ),
  361. 'baz' => 'Foo Baz',
  362. ),
  363. );
  364. return array(
  365. array('Symfony est super!', $messages, 'symfony.is.great'),
  366. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  367. array('Foo Baz', $messages, 'foo.baz'),
  368. );
  369. }
  370. public function getTransChoiceTests()
  371. {
  372. return array(
  373. 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(), 'fr', ''),
  374. 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(), 'fr', ''),
  375. 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(), 'fr', ''),
  376. 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(), 'fr', ''),
  377. 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(), 'fr', ''),
  378. 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(), 'fr', ''),
  379. 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(), 'fr', ''),
  380. 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(), 'fr', ''),
  381. 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(), 'fr', ''),
  382. 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(), 'fr', ''),
  383. 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(), 'fr', ''),
  384. 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(), 'fr', ''),
  385. 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(), 'fr', ''),
  386. // Override %count% with a custom value
  387. array('Il y a quelques pommes', 'one: There is one apple|more: There are %count% apples', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 2, array('%count%' => 'quelques'), 'fr', ''),
  388. );
  389. }
  390. public function getInvalidLocalesTests()
  391. {
  392. return array(
  393. array('fr FR'),
  394. array('français'),
  395. array('fr+en'),
  396. array('utf#8'),
  397. array('fr&en'),
  398. array('fr~FR'),
  399. array(' fr'),
  400. array('fr '),
  401. array('fr*'),
  402. array('fr/FR'),
  403. array('fr\\FR'),
  404. );
  405. }
  406. public function getValidLocalesTests()
  407. {
  408. return array(
  409. array(''),
  410. array(null),
  411. array('fr'),
  412. array('francais'),
  413. array('FR'),
  414. array('frFR'),
  415. array('fr-FR'),
  416. array('fr_FR'),
  417. array('fr.FR'),
  418. array('fr-FR.UTF8'),
  419. array('sr@latin'),
  420. );
  421. }
  422. public function testTransChoiceFallback()
  423. {
  424. $translator = new Translator('ru');
  425. $translator->setFallbackLocales(array('en'));
  426. $translator->addLoader('array', new ArrayLoader());
  427. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
  428. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  429. }
  430. public function testTransChoiceFallbackBis()
  431. {
  432. $translator = new Translator('ru');
  433. $translator->setFallbackLocales(array('en_US', 'en'));
  434. $translator->addLoader('array', new ArrayLoader());
  435. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
  436. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  437. }
  438. public function testTransChoiceFallbackWithNoTranslation()
  439. {
  440. $translator = new Translator('ru');
  441. $translator->setFallbackLocales(array('en'));
  442. $translator->addLoader('array', new ArrayLoader());
  443. // consistent behavior with Translator::trans(), which returns the string
  444. // unchanged if it can't be found
  445. $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  446. }
  447. }
  448. class StringClass
  449. {
  450. protected $str;
  451. public function __construct($str)
  452. {
  453. $this->str = $str;
  454. }
  455. public function __toString()
  456. {
  457. return $this->str;
  458. }
  459. }