No Description

TranslatorTest.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 testSetFallbackLocales()
  71. {
  72. $translator = new Translator('en');
  73. $translator->addLoader('array', new ArrayLoader());
  74. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  75. $translator->addResource('array', array('bar' => 'foobar'), 'fr');
  76. // force catalogue loading
  77. $translator->trans('bar');
  78. $translator->setFallbackLocales(array('fr'));
  79. $this->assertEquals('foobar', $translator->trans('bar'));
  80. }
  81. public function testSetFallbackLocalesMultiple()
  82. {
  83. $translator = new Translator('en');
  84. $translator->addLoader('array', new ArrayLoader());
  85. $translator->addResource('array', array('foo' => 'foo (en)'), 'en');
  86. $translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
  87. // force catalogue loading
  88. $translator->trans('bar');
  89. $translator->setFallbackLocales(array('fr_FR', 'fr'));
  90. $this->assertEquals('bar (fr)', $translator->trans('bar'));
  91. }
  92. /**
  93. * @dataProvider getInvalidLocalesTests
  94. * @expectedException \InvalidArgumentException
  95. */
  96. public function testSetFallbackInvalidLocales($locale)
  97. {
  98. $translator = new Translator('fr', new MessageSelector());
  99. $translator->setFallbackLocales(array('fr', $locale));
  100. }
  101. /**
  102. * @dataProvider getValidLocalesTests
  103. */
  104. public function testSetFallbackValidLocales($locale)
  105. {
  106. $translator = new Translator($locale, new MessageSelector());
  107. $translator->setFallbackLocales(array('fr', $locale));
  108. // no assertion. this method just asserts that no exception is thrown
  109. }
  110. public function testTransWithFallbackLocale()
  111. {
  112. $translator = new Translator('fr_FR');
  113. $translator->addLoader('array', new ArrayLoader());
  114. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  115. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  116. $translator->setFallbackLocales(array('en'));
  117. $this->assertEquals('foobar', $translator->trans('bar'));
  118. }
  119. /**
  120. * @dataProvider getInvalidLocalesTests
  121. * @expectedException \InvalidArgumentException
  122. */
  123. public function testAddResourceInvalidLocales($locale)
  124. {
  125. $translator = new Translator('fr', new MessageSelector());
  126. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  127. }
  128. /**
  129. * @dataProvider getValidLocalesTests
  130. */
  131. public function testAddResourceValidLocales($locale)
  132. {
  133. $translator = new Translator('fr', new MessageSelector());
  134. $translator->addResource('array', array('foo' => 'foofoo'), $locale);
  135. // no assertion. this method just asserts that no exception is thrown
  136. }
  137. public function testAddResourceAfterTrans()
  138. {
  139. $translator = new Translator('fr');
  140. $translator->addLoader('array', new ArrayLoader());
  141. $translator->setFallbackLocales(array('en'));
  142. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  143. $this->assertEquals('foofoo', $translator->trans('foo'));
  144. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  145. $this->assertEquals('foobar', $translator->trans('bar'));
  146. }
  147. /**
  148. * @dataProvider getTransFileTests
  149. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  150. */
  151. public function testTransWithoutFallbackLocaleFile($format, $loader)
  152. {
  153. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  154. $translator = new Translator('en');
  155. $translator->addLoader($format, new $loaderClass());
  156. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en');
  157. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en');
  158. // force catalogue loading
  159. $translator->trans('foo');
  160. }
  161. /**
  162. * @dataProvider getTransFileTests
  163. */
  164. public function testTransWithFallbackLocaleFile($format, $loader)
  165. {
  166. $loaderClass = 'Symfony\\Component\\Translation\\Loader\\'.$loader;
  167. $translator = new Translator('en_GB');
  168. $translator->addLoader($format, new $loaderClass());
  169. $translator->addResource($format, __DIR__.'/fixtures/non-existing', 'en_GB');
  170. $translator->addResource($format, __DIR__.'/fixtures/resources.'.$format, 'en', 'resources');
  171. $this->assertEquals('bar', $translator->trans('foo', array(), 'resources'));
  172. }
  173. public function testTransWithFallbackLocaleBis()
  174. {
  175. $translator = new Translator('en_US');
  176. $translator->addLoader('array', new ArrayLoader());
  177. $translator->addResource('array', array('foo' => 'foofoo'), 'en_US');
  178. $translator->addResource('array', array('bar' => 'foobar'), 'en');
  179. $this->assertEquals('foobar', $translator->trans('bar'));
  180. }
  181. public function testTransWithFallbackLocaleTer()
  182. {
  183. $translator = new Translator('fr_FR');
  184. $translator->addLoader('array', new ArrayLoader());
  185. $translator->addResource('array', array('foo' => 'foo (en_US)'), 'en_US');
  186. $translator->addResource('array', array('bar' => 'bar (en)'), 'en');
  187. $translator->setFallbackLocales(array('en_US', 'en'));
  188. $this->assertEquals('foo (en_US)', $translator->trans('foo'));
  189. $this->assertEquals('bar (en)', $translator->trans('bar'));
  190. }
  191. public function testTransNonExistentWithFallback()
  192. {
  193. $translator = new Translator('fr');
  194. $translator->setFallbackLocales(array('en'));
  195. $translator->addLoader('array', new ArrayLoader());
  196. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  197. }
  198. /**
  199. * @expectedException \RuntimeException
  200. */
  201. public function testWhenAResourceHasNoRegisteredLoader()
  202. {
  203. $translator = new Translator('en');
  204. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  205. $translator->trans('foo');
  206. }
  207. /**
  208. * @dataProvider getTransTests
  209. */
  210. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  211. {
  212. $translator = new Translator('en');
  213. $translator->addLoader('array', new ArrayLoader());
  214. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  215. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  216. }
  217. /**
  218. * @dataProvider getInvalidLocalesTests
  219. * @expectedException \InvalidArgumentException
  220. */
  221. public function testTransInvalidLocale($locale)
  222. {
  223. $translator = new Translator('en', new MessageSelector());
  224. $translator->addLoader('array', new ArrayLoader());
  225. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  226. $translator->trans('foo', array(), '', $locale);
  227. }
  228. /**
  229. * @dataProvider getValidLocalesTests
  230. */
  231. public function testTransValidLocale($locale)
  232. {
  233. $translator = new Translator('en', new MessageSelector());
  234. $translator->addLoader('array', new ArrayLoader());
  235. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  236. $translator->trans('foo', array(), '', $locale);
  237. // no assertion. this method just asserts that no exception is thrown
  238. }
  239. /**
  240. * @dataProvider getFlattenedTransTests
  241. */
  242. public function testFlattenedTrans($expected, $messages, $id)
  243. {
  244. $translator = new Translator('en');
  245. $translator->addLoader('array', new ArrayLoader());
  246. $translator->addResource('array', $messages, 'fr', '');
  247. $this->assertEquals($expected, $translator->trans($id, array(), '', 'fr'));
  248. }
  249. /**
  250. * @dataProvider getTransChoiceTests
  251. */
  252. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  253. {
  254. $translator = new Translator('en');
  255. $translator->addLoader('array', new ArrayLoader());
  256. $translator->addResource('array', array((string) $id => $translation), $locale, $domain);
  257. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  258. }
  259. /**
  260. * @dataProvider getInvalidLocalesTests
  261. * @expectedException \InvalidArgumentException
  262. */
  263. public function testTransChoiceInvalidLocale($locale)
  264. {
  265. $translator = new Translator('en', new MessageSelector());
  266. $translator->addLoader('array', new ArrayLoader());
  267. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  268. $translator->transChoice('foo', 1, array(), '', $locale);
  269. }
  270. /**
  271. * @dataProvider getValidLocalesTests
  272. */
  273. public function testTransChoiceValidLocale($locale)
  274. {
  275. $translator = new Translator('en', new MessageSelector());
  276. $translator->addLoader('array', new ArrayLoader());
  277. $translator->addResource('array', array('foo' => 'foofoo'), 'en');
  278. $translator->transChoice('foo', 1, array(), '', $locale);
  279. // no assertion. this method just asserts that no exception is thrown
  280. }
  281. public function getTransFileTests()
  282. {
  283. return array(
  284. array('csv', 'CsvFileLoader'),
  285. array('ini', 'IniFileLoader'),
  286. array('mo', 'MoFileLoader'),
  287. array('po', 'PoFileLoader'),
  288. array('php', 'PhpFileLoader'),
  289. array('ts', 'QtFileLoader'),
  290. array('xlf', 'XliffFileLoader'),
  291. array('yml', 'YamlFileLoader'),
  292. array('json', 'JsonFileLoader'),
  293. );
  294. }
  295. public function getTransTests()
  296. {
  297. return array(
  298. array('Symfony est super !', 'Symfony is great!', 'Symfony est super !', array(), 'fr', ''),
  299. array('Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', array('%what%' => 'awesome'), 'fr', ''),
  300. array('Symfony est super !', new String('Symfony is great!'), 'Symfony est super !', array(), 'fr', ''),
  301. );
  302. }
  303. public function getFlattenedTransTests()
  304. {
  305. $messages = array(
  306. 'symfony' => array(
  307. 'is' => array(
  308. 'great' => 'Symfony est super!',
  309. ),
  310. ),
  311. 'foo' => array(
  312. 'bar' => array(
  313. 'baz' => 'Foo Bar Baz',
  314. ),
  315. 'baz' => 'Foo Baz',
  316. ),
  317. );
  318. return array(
  319. array('Symfony est super!', $messages, 'symfony.is.great'),
  320. array('Foo Bar Baz', $messages, 'foo.bar.baz'),
  321. array('Foo Baz', $messages, 'foo.baz'),
  322. );
  323. }
  324. public function getTransChoiceTests()
  325. {
  326. return array(
  327. 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', ''),
  328. 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', ''),
  329. 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', ''),
  330. 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', ''),
  331. 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', ''),
  332. 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', ''),
  333. 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', ''),
  334. 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', ''),
  335. 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', ''),
  336. 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', ''),
  337. 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', ''),
  338. 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', ''),
  339. 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', ''),
  340. );
  341. }
  342. public function getInvalidLocalesTests()
  343. {
  344. return array(
  345. array('fr FR'),
  346. array('français'),
  347. array('fr+en'),
  348. array('utf#8'),
  349. array('fr&en'),
  350. array('fr~FR'),
  351. array(' fr'),
  352. array('fr '),
  353. array('fr*'),
  354. array('fr/FR'),
  355. array('fr\\FR'),
  356. );
  357. }
  358. public function getValidLocalesTests()
  359. {
  360. return array(
  361. array(''),
  362. array(null),
  363. array('fr'),
  364. array('francais'),
  365. array('FR'),
  366. array('frFR'),
  367. array('fr-FR'),
  368. array('fr_FR'),
  369. array('fr.FR'),
  370. array('fr-FR.UTF8'),
  371. array('sr@latin'),
  372. );
  373. }
  374. public function testTransChoiceFallback()
  375. {
  376. $translator = new Translator('ru');
  377. $translator->setFallbackLocales(array('en'));
  378. $translator->addLoader('array', new ArrayLoader());
  379. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en');
  380. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  381. }
  382. public function testTransChoiceFallbackBis()
  383. {
  384. $translator = new Translator('ru');
  385. $translator->setFallbackLocales(array('en_US', 'en'));
  386. $translator->addLoader('array', new ArrayLoader());
  387. $translator->addResource('array', array('some_message2' => 'one thing|%count% things'), 'en_US');
  388. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  389. }
  390. public function testTransChoiceFallbackWithNoTranslation()
  391. {
  392. $translator = new Translator('ru');
  393. $translator->setFallbackLocales(array('en'));
  394. $translator->addLoader('array', new ArrayLoader());
  395. // consistent behavior with Translator::trans(), which returns the string
  396. // unchanged if it can't be found
  397. $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, array('%count%' => 10)));
  398. }
  399. /**
  400. * @dataProvider dataProviderGetMessages
  401. */
  402. public function testGetMessages($resources, $locale, $expected)
  403. {
  404. $locales = array_keys($resources);
  405. $_locale = !is_null($locale) ? $locale : reset($locales);
  406. $locales = array_slice($locales, 0, array_search($_locale, $locales));
  407. $translator = new Translator($_locale, new MessageSelector());
  408. $translator->setFallbackLocales(array_reverse($locales));
  409. $translator->addLoader('array', new ArrayLoader());
  410. foreach ($resources as $_locale => $domainMessages) {
  411. foreach ($domainMessages as $domain => $messages) {
  412. $translator->addResource('array', $messages, $_locale, $domain);
  413. }
  414. }
  415. $result = $translator->getMessages($locale);
  416. $this->assertEquals($expected, $result);
  417. }
  418. public function dataProviderGetMessages()
  419. {
  420. $resources = array(
  421. 'en' => array(
  422. 'jsmessages' => array(
  423. 'foo' => 'foo (EN)',
  424. 'bar' => 'bar (EN)',
  425. ),
  426. 'messages' => array(
  427. 'foo' => 'foo messages (EN)',
  428. ),
  429. 'validators' => array(
  430. 'int' => 'integer (EN)',
  431. ),
  432. ),
  433. 'pt-PT' => array(
  434. 'messages' => array(
  435. 'foo' => 'foo messages (PT)',
  436. ),
  437. 'validators' => array(
  438. 'str' => 'integer (PT)',
  439. ),
  440. ),
  441. 'pt_BR' => array(
  442. 'validators' => array(
  443. 'int' => 'integer (BR)',
  444. ),
  445. ),
  446. );
  447. return array(
  448. array($resources, null,
  449. array(
  450. 'jsmessages' => array(
  451. 'foo' => 'foo (EN)',
  452. 'bar' => 'bar (EN)',
  453. ),
  454. 'messages' => array(
  455. 'foo' => 'foo messages (EN)',
  456. ),
  457. 'validators' => array(
  458. 'int' => 'integer (EN)',
  459. ),
  460. ),
  461. ),
  462. array($resources, 'en',
  463. array(
  464. 'jsmessages' => array(
  465. 'foo' => 'foo (EN)',
  466. 'bar' => 'bar (EN)',
  467. ),
  468. 'messages' => array(
  469. 'foo' => 'foo messages (EN)',
  470. ),
  471. 'validators' => array(
  472. 'int' => 'integer (EN)',
  473. ),
  474. ),
  475. ),
  476. array($resources, 'pt-PT',
  477. array(
  478. 'jsmessages' => array(
  479. 'foo' => 'foo (EN)',
  480. 'bar' => 'bar (EN)',
  481. ),
  482. 'messages' => array(
  483. 'foo' => 'foo messages (PT)',
  484. ),
  485. 'validators' => array(
  486. 'int' => 'integer (EN)',
  487. 'str' => 'integer (PT)',
  488. ),
  489. ),
  490. ),
  491. array($resources, 'pt_BR',
  492. array(
  493. 'jsmessages' => array(
  494. 'foo' => 'foo (EN)',
  495. 'bar' => 'bar (EN)',
  496. ),
  497. 'messages' => array(
  498. 'foo' => 'foo messages (PT)',
  499. ),
  500. 'validators' => array(
  501. 'int' => 'integer (BR)',
  502. 'str' => 'integer (PT)',
  503. ),
  504. ),
  505. ),
  506. );
  507. }
  508. }
  509. class String
  510. {
  511. protected $str;
  512. public function __construct($str)
  513. {
  514. $this->str = $str;
  515. }
  516. public function __toString()
  517. {
  518. return $this->str;
  519. }
  520. }