TranslatorTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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\Loader\ArrayLoader;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. use Symfony\Component\Translation\Translator;
  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', ['foo' => 'foofoo'], $locale, 'domain-a');
  82. $translator->addResource('loader-b', ['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', ['foo' => 'foofoo'], 'en');
  96. $translator->addResource('array', ['bar' => 'foobar'], 'fr');
  97. // force catalogue loading
  98. $translator->trans('bar');
  99. $translator->setFallbackLocales(['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', ['foo' => 'foo (en)'], 'en');
  107. $translator->addResource('array', ['bar' => 'bar (fr)'], 'fr');
  108. // force catalogue loading
  109. $translator->trans('bar');
  110. $translator->setFallbackLocales(['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(['fr', $locale]);
  121. }
  122. /**
  123. * @dataProvider getValidLocalesTests
  124. */
  125. public function testSetFallbackValidLocales($locale)
  126. {
  127. $translator = new Translator($locale);
  128. $translator->setFallbackLocales(['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(['en']);
  136. $translator->addLoader('array', new ArrayLoader());
  137. $translator->addResource('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', ['foo' => 'foofoo'], $locale);
  148. }
  149. /**
  150. * @dataProvider getValidLocalesTests
  151. */
  152. public function testAddResourceValidLocales($locale)
  153. {
  154. $translator = new Translator('fr');
  155. $translator->addResource('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(['en']);
  164. $translator->addResource('array', ['foo' => 'foofoo'], 'en');
  165. $this->assertEquals('foofoo', $translator->trans('foo'));
  166. $translator->addResource('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', [], 'resources'));
  194. }
  195. public function testTransWithIcuFallbackLocale()
  196. {
  197. $translator = new Translator('en_GB');
  198. $translator->addLoader('array', new ArrayLoader());
  199. $translator->addResource('array', ['foo' => 'foofoo'], 'en_GB');
  200. $translator->addResource('array', ['bar' => 'foobar'], 'en_001');
  201. $translator->addResource('array', ['baz' => 'foobaz'], 'en');
  202. $this->assertSame('foofoo', $translator->trans('foo'));
  203. $this->assertSame('foobar', $translator->trans('bar'));
  204. $this->assertSame('foobaz', $translator->trans('baz'));
  205. }
  206. public function testTransWithIcuVariantFallbackLocale()
  207. {
  208. $translator = new Translator('en_GB_scouse');
  209. $translator->addLoader('array', new ArrayLoader());
  210. $translator->addResource('array', ['foo' => 'foofoo'], 'en_GB_scouse');
  211. $translator->addResource('array', ['bar' => 'foobar'], 'en_GB');
  212. $translator->addResource('array', ['baz' => 'foobaz'], 'en_001');
  213. $translator->addResource('array', ['qux' => 'fooqux'], 'en');
  214. $this->assertSame('foofoo', $translator->trans('foo'));
  215. $this->assertSame('foobar', $translator->trans('bar'));
  216. $this->assertSame('foobaz', $translator->trans('baz'));
  217. $this->assertSame('fooqux', $translator->trans('qux'));
  218. }
  219. public function testTransWithIcuRootFallbackLocale()
  220. {
  221. $translator = new Translator('az_Cyrl');
  222. $translator->addLoader('array', new ArrayLoader());
  223. $translator->addResource('array', ['foo' => 'foofoo'], 'az_Cyrl');
  224. $translator->addResource('array', ['bar' => 'foobar'], 'az');
  225. $this->assertSame('foofoo', $translator->trans('foo'));
  226. $this->assertSame('bar', $translator->trans('bar'));
  227. }
  228. public function testTransWithFallbackLocaleBis()
  229. {
  230. $translator = new Translator('en_US');
  231. $translator->addLoader('array', new ArrayLoader());
  232. $translator->addResource('array', ['foo' => 'foofoo'], 'en_US');
  233. $translator->addResource('array', ['bar' => 'foobar'], 'en');
  234. $this->assertEquals('foobar', $translator->trans('bar'));
  235. }
  236. public function testTransWithFallbackLocaleTer()
  237. {
  238. $translator = new Translator('fr_FR');
  239. $translator->addLoader('array', new ArrayLoader());
  240. $translator->addResource('array', ['foo' => 'foo (en_US)'], 'en_US');
  241. $translator->addResource('array', ['bar' => 'bar (en)'], 'en');
  242. $translator->setFallbackLocales(['en_US', 'en']);
  243. $this->assertEquals('foo (en_US)', $translator->trans('foo'));
  244. $this->assertEquals('bar (en)', $translator->trans('bar'));
  245. }
  246. public function testTransNonExistentWithFallback()
  247. {
  248. $translator = new Translator('fr');
  249. $translator->setFallbackLocales(['en']);
  250. $translator->addLoader('array', new ArrayLoader());
  251. $this->assertEquals('non-existent', $translator->trans('non-existent'));
  252. }
  253. /**
  254. * @expectedException \Symfony\Component\Translation\Exception\RuntimeException
  255. */
  256. public function testWhenAResourceHasNoRegisteredLoader()
  257. {
  258. $translator = new Translator('en');
  259. $translator->addResource('array', ['foo' => 'foofoo'], 'en');
  260. $translator->trans('foo');
  261. }
  262. public function testNestedFallbackCatalogueWhenUsingMultipleLocales()
  263. {
  264. $translator = new Translator('fr');
  265. $translator->setFallbackLocales(['ru', 'en']);
  266. $translator->getCatalogue('fr');
  267. $this->assertNotNull($translator->getCatalogue('ru')->getFallbackCatalogue());
  268. }
  269. public function testFallbackCatalogueResources()
  270. {
  271. $translator = new Translator('en_GB');
  272. $translator->addLoader('yml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
  273. $translator->addResource('yml', __DIR__.'/fixtures/empty.yml', 'en_GB');
  274. $translator->addResource('yml', __DIR__.'/fixtures/resources.yml', 'en');
  275. // force catalogue loading
  276. $this->assertEquals('bar', $translator->trans('foo', []));
  277. $resources = $translator->getCatalogue('en')->getResources();
  278. $this->assertCount(1, $resources);
  279. $this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'resources.yml', $resources);
  280. $resources = $translator->getCatalogue('en_GB')->getResources();
  281. $this->assertCount(2, $resources);
  282. $this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'empty.yml', $resources);
  283. $this->assertContains(__DIR__.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'resources.yml', $resources);
  284. }
  285. /**
  286. * @dataProvider getTransTests
  287. */
  288. public function testTrans($expected, $id, $translation, $parameters, $locale, $domain)
  289. {
  290. $translator = new Translator('en');
  291. $translator->addLoader('array', new ArrayLoader());
  292. $translator->addResource('array', [(string) $id => $translation], $locale, $domain);
  293. $this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
  294. }
  295. /**
  296. * @dataProvider getInvalidLocalesTests
  297. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  298. */
  299. public function testTransInvalidLocale($locale)
  300. {
  301. $translator = new Translator('en');
  302. $translator->addLoader('array', new ArrayLoader());
  303. $translator->addResource('array', ['foo' => 'foofoo'], 'en');
  304. $translator->trans('foo', [], '', $locale);
  305. }
  306. /**
  307. * @dataProvider getValidLocalesTests
  308. */
  309. public function testTransValidLocale($locale)
  310. {
  311. $translator = new Translator($locale);
  312. $translator->addLoader('array', new ArrayLoader());
  313. $translator->addResource('array', ['test' => 'OK'], $locale);
  314. $this->assertEquals('OK', $translator->trans('test'));
  315. $this->assertEquals('OK', $translator->trans('test', [], null, $locale));
  316. }
  317. /**
  318. * @dataProvider getFlattenedTransTests
  319. */
  320. public function testFlattenedTrans($expected, $messages, $id)
  321. {
  322. $translator = new Translator('en');
  323. $translator->addLoader('array', new ArrayLoader());
  324. $translator->addResource('array', $messages, 'fr', '');
  325. $this->assertEquals($expected, $translator->trans($id, [], '', 'fr'));
  326. }
  327. /**
  328. * @dataProvider getTransChoiceTests
  329. * @group legacy
  330. */
  331. public function testTransChoice($expected, $id, $translation, $number, $parameters, $locale, $domain)
  332. {
  333. $translator = new Translator('en');
  334. $translator->addLoader('array', new ArrayLoader());
  335. $translator->addResource('array', [(string) $id => $translation], $locale, $domain);
  336. $this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
  337. }
  338. /**
  339. * @dataProvider getInvalidLocalesTests
  340. * @expectedException \Symfony\Component\Translation\Exception\InvalidArgumentException
  341. * @group legacy
  342. */
  343. public function testTransChoiceInvalidLocale($locale)
  344. {
  345. $translator = new Translator('en');
  346. $translator->addLoader('array', new ArrayLoader());
  347. $translator->addResource('array', ['foo' => 'foofoo'], 'en');
  348. $translator->transChoice('foo', 1, [], '', $locale);
  349. }
  350. /**
  351. * @dataProvider getValidLocalesTests
  352. * @group legacy
  353. */
  354. public function testTransChoiceValidLocale($locale)
  355. {
  356. $translator = new Translator('en');
  357. $translator->addLoader('array', new ArrayLoader());
  358. $translator->addResource('array', ['foo' => 'foofoo'], 'en');
  359. $translator->transChoice('foo', 1, [], '', $locale);
  360. // no assertion. this method just asserts that no exception is thrown
  361. $this->addToAssertionCount(1);
  362. }
  363. public function getTransFileTests()
  364. {
  365. return [
  366. ['csv', 'CsvFileLoader'],
  367. ['ini', 'IniFileLoader'],
  368. ['mo', 'MoFileLoader'],
  369. ['po', 'PoFileLoader'],
  370. ['php', 'PhpFileLoader'],
  371. ['ts', 'QtFileLoader'],
  372. ['xlf', 'XliffFileLoader'],
  373. ['yml', 'YamlFileLoader'],
  374. ['json', 'JsonFileLoader'],
  375. ];
  376. }
  377. public function getTransTests()
  378. {
  379. return [
  380. ['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
  381. ['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
  382. ['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
  383. ];
  384. }
  385. public function getFlattenedTransTests()
  386. {
  387. $messages = [
  388. 'symfony' => [
  389. 'is' => [
  390. 'great' => 'Symfony est super!',
  391. ],
  392. ],
  393. 'foo' => [
  394. 'bar' => [
  395. 'baz' => 'Foo Bar Baz',
  396. ],
  397. 'baz' => 'Foo Baz',
  398. ],
  399. ];
  400. return [
  401. ['Symfony est super!', $messages, 'symfony.is.great'],
  402. ['Foo Bar Baz', $messages, 'foo.bar.baz'],
  403. ['Foo Baz', $messages, 'foo.baz'],
  404. ];
  405. }
  406. public function getTransChoiceTests()
  407. {
  408. return [
  409. ['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, [], 'fr', ''],
  410. ['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, [], 'fr', ''],
  411. ['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, [], 'fr', ''],
  412. ['Il y a 0 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 0, [], 'fr', ''],
  413. ['Il y a 1 pomme', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 1, [], 'fr', ''],
  414. ['Il y a 10 pommes', 'There is one apple|There is %count% apples', 'Il y a %count% pomme|Il y a %count% pommes', 10, [], 'fr', ''],
  415. ['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, [], 'fr', ''],
  416. ['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, [], 'fr', ''],
  417. ['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, [], 'fr', ''],
  418. ['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, [], 'fr', ''],
  419. ['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, [], 'fr', ''],
  420. ['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, [], 'fr', ''],
  421. ['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, [], 'fr', ''],
  422. // Override %count% with a custom value
  423. ['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 quelques pommes', 2, ['%count%' => 'quelques'], 'fr', ''],
  424. ];
  425. }
  426. public function getInvalidLocalesTests()
  427. {
  428. return [
  429. ['fr FR'],
  430. ['français'],
  431. ['fr+en'],
  432. ['utf#8'],
  433. ['fr&en'],
  434. ['fr~FR'],
  435. [' fr'],
  436. ['fr '],
  437. ['fr*'],
  438. ['fr/FR'],
  439. ['fr\\FR'],
  440. ];
  441. }
  442. public function getValidLocalesTests()
  443. {
  444. return [
  445. [''],
  446. [null],
  447. ['fr'],
  448. ['francais'],
  449. ['FR'],
  450. ['frFR'],
  451. ['fr-FR'],
  452. ['fr_FR'],
  453. ['fr.FR'],
  454. ['fr-FR.UTF8'],
  455. ['sr@latin'],
  456. ];
  457. }
  458. /**
  459. * @requires extension intl
  460. */
  461. public function testIntlFormattedDomain()
  462. {
  463. $translator = new Translator('en');
  464. $translator->addLoader('array', new ArrayLoader());
  465. $translator->addResource('array', ['some_message' => 'Hello %name%'], 'en');
  466. $this->assertSame('Hello Bob', $translator->trans('some_message', ['%name%' => 'Bob']));
  467. $translator->addResource('array', ['some_message' => 'Hi {name}'], 'en', 'messages+intl-icu');
  468. $this->assertSame('Hi Bob', $translator->trans('some_message', ['%name%' => 'Bob']));
  469. }
  470. /**
  471. * @group legacy
  472. */
  473. public function testTransChoiceFallback()
  474. {
  475. $translator = new Translator('ru');
  476. $translator->setFallbackLocales(['en']);
  477. $translator->addLoader('array', new ArrayLoader());
  478. $translator->addResource('array', ['some_message2' => 'one thing|%count% things'], 'en');
  479. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, ['%count%' => 10]));
  480. }
  481. /**
  482. * @group legacy
  483. */
  484. public function testTransChoiceFallbackBis()
  485. {
  486. $translator = new Translator('ru');
  487. $translator->setFallbackLocales(['en_US', 'en']);
  488. $translator->addLoader('array', new ArrayLoader());
  489. $translator->addResource('array', ['some_message2' => 'one thing|%count% things'], 'en_US');
  490. $this->assertEquals('10 things', $translator->transChoice('some_message2', 10, ['%count%' => 10]));
  491. }
  492. /**
  493. * @group legacy
  494. */
  495. public function testTransChoiceFallbackWithNoTranslation()
  496. {
  497. $translator = new Translator('ru');
  498. $translator->setFallbackLocales(['en']);
  499. $translator->addLoader('array', new ArrayLoader());
  500. // consistent behavior with Translator::trans(), which returns the string
  501. // unchanged if it can't be found
  502. $this->assertEquals('some_message2', $translator->transChoice('some_message2', 10, ['%count%' => 10]));
  503. }
  504. }
  505. class StringClass
  506. {
  507. protected $str;
  508. public function __construct($str)
  509. {
  510. $this->str = $str;
  511. }
  512. public function __toString()
  513. {
  514. return $this->str;
  515. }
  516. }