菜谱项目

TranslatorTest.php 21KB

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