Aucune description

TranslatorTest.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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\Contracts\Translation\Test;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. use Symfony\Contracts\Translation\TranslatorTrait;
  14. /**
  15. * Test should cover all languages mentioned on http://translate.sourceforge.net/wiki/l10n/pluralforms
  16. * and Plural forms mentioned on http://www.gnu.org/software/gettext/manual/gettext.html#Plural-forms.
  17. *
  18. * See also https://developer.mozilla.org/en/Localization_and_Plurals which mentions 15 rules having a maximum of 6 forms.
  19. * The mozilla code is also interesting to check for.
  20. *
  21. * As mentioned by chx http://drupal.org/node/1273968 we can cover all by testing number from 0 to 199
  22. *
  23. * The goal to cover all languages is to far fetched so this test case is smaller.
  24. *
  25. * @author Clemens Tolboom clemens@build2be.nl
  26. */
  27. class TranslatorTest extends TestCase
  28. {
  29. private $defaultLocale;
  30. protected function setUp(): void
  31. {
  32. $this->defaultLocale = \Locale::getDefault();
  33. \Locale::setDefault('en');
  34. }
  35. protected function tearDown(): void
  36. {
  37. \Locale::setDefault($this->defaultLocale);
  38. }
  39. /**
  40. * @return TranslatorInterface
  41. */
  42. public function getTranslator()
  43. {
  44. return new class() implements TranslatorInterface {
  45. use TranslatorTrait;
  46. };
  47. }
  48. /**
  49. * @dataProvider getTransTests
  50. */
  51. public function testTrans($expected, $id, $parameters)
  52. {
  53. $translator = $this->getTranslator();
  54. $this->assertEquals($expected, $translator->trans($id, $parameters));
  55. }
  56. /**
  57. * @dataProvider getTransChoiceTests
  58. */
  59. public function testTransChoiceWithExplicitLocale($expected, $id, $number)
  60. {
  61. $translator = $this->getTranslator();
  62. $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
  63. }
  64. /**
  65. * @requires extension intl
  66. *
  67. * @dataProvider getTransChoiceTests
  68. */
  69. public function testTransChoiceWithDefaultLocale($expected, $id, $number)
  70. {
  71. $translator = $this->getTranslator();
  72. $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
  73. }
  74. /**
  75. * @dataProvider getTransChoiceTests
  76. */
  77. public function testTransChoiceWithEnUsPosix($expected, $id, $number)
  78. {
  79. $translator = $this->getTranslator();
  80. $translator->setLocale('en_US_POSIX');
  81. $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number]));
  82. }
  83. public function testGetSetLocale()
  84. {
  85. $translator = $this->getTranslator();
  86. $this->assertEquals('en', $translator->getLocale());
  87. }
  88. /**
  89. * @requires extension intl
  90. */
  91. public function testGetLocaleReturnsDefaultLocaleIfNotSet()
  92. {
  93. $translator = $this->getTranslator();
  94. \Locale::setDefault('pt_BR');
  95. $this->assertEquals('pt_BR', $translator->getLocale());
  96. \Locale::setDefault('en');
  97. $this->assertEquals('en', $translator->getLocale());
  98. }
  99. public function getTransTests()
  100. {
  101. return [
  102. ['Symfony is great!', 'Symfony is great!', []],
  103. ['Symfony is awesome!', 'Symfony is %what%!', ['%what%' => 'awesome']],
  104. ];
  105. }
  106. public function getTransChoiceTests()
  107. {
  108. return [
  109. ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
  110. ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1],
  111. ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
  112. ['There are 0 apples', 'There is 1 apple|There are %count% apples', 0],
  113. ['There is 1 apple', 'There is 1 apple|There are %count% apples', 1],
  114. ['There are 10 apples', 'There is 1 apple|There are %count% apples', 10],
  115. // custom validation messages may be coded with a fixed value
  116. ['There are 2 apples', 'There are 2 apples', 2],
  117. ];
  118. }
  119. /**
  120. * @dataProvider getInternal
  121. */
  122. public function testInterval($expected, $number, $interval)
  123. {
  124. $translator = $this->getTranslator();
  125. $this->assertEquals($expected, $translator->trans($interval.' foo|[1,Inf[ bar', ['%count%' => $number]));
  126. }
  127. public function getInternal()
  128. {
  129. return [
  130. ['foo', 3, '{1,2, 3 ,4}'],
  131. ['bar', 10, '{1,2, 3 ,4}'],
  132. ['bar', 3, '[1,2]'],
  133. ['foo', 1, '[1,2]'],
  134. ['foo', 2, '[1,2]'],
  135. ['bar', 1, ']1,2['],
  136. ['bar', 2, ']1,2['],
  137. ['foo', log(0), '[-Inf,2['],
  138. ['foo', -log(0), '[-2,+Inf]'],
  139. ];
  140. }
  141. /**
  142. * @dataProvider getChooseTests
  143. */
  144. public function testChoose($expected, $id, $number, $locale = null)
  145. {
  146. $translator = $this->getTranslator();
  147. $this->assertEquals($expected, $translator->trans($id, ['%count%' => $number], null, $locale));
  148. }
  149. public function testReturnMessageIfExactlyOneStandardRuleIsGiven()
  150. {
  151. $translator = $this->getTranslator();
  152. $this->assertEquals('There are two apples', $translator->trans('There are two apples', ['%count%' => 2]));
  153. }
  154. /**
  155. * @dataProvider getNonMatchingMessages
  156. */
  157. public function testThrowExceptionIfMatchingMessageCannotBeFound($id, $number)
  158. {
  159. $this->expectException(\InvalidArgumentException::class);
  160. $translator = $this->getTranslator();
  161. $translator->trans($id, ['%count%' => $number]);
  162. }
  163. public function getNonMatchingMessages()
  164. {
  165. return [
  166. ['{0} There are no apples|{1} There is one apple', 2],
  167. ['{1} There is one apple|]1,Inf] There are %count% apples', 0],
  168. ['{1} There is one apple|]2,Inf] There are %count% apples', 2],
  169. ['{0} There are no apples|There is one apple', 2],
  170. ];
  171. }
  172. public function getChooseTests()
  173. {
  174. return [
  175. ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
  176. ['There are no apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
  177. ['There are no apples', '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 0],
  178. ['There is one apple', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 1],
  179. ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
  180. ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples', 10],
  181. ['There are 10 apples', '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples', 10],
  182. ['There are 0 apples', 'There is one apple|There are %count% apples', 0],
  183. ['There is one apple', 'There is one apple|There are %count% apples', 1],
  184. ['There are 10 apples', 'There is one apple|There are %count% apples', 10],
  185. ['There are 0 apples', 'one: There is one apple|more: There are %count% apples', 0],
  186. ['There is one apple', 'one: There is one apple|more: There are %count% apples', 1],
  187. ['There are 10 apples', 'one: There is one apple|more: There are %count% apples', 10],
  188. ['There are no apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 0],
  189. ['There is one apple', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 1],
  190. ['There are 10 apples', '{0} There are no apples|one: There is one apple|more: There are %count% apples', 10],
  191. ['', '{0}|{1} There is one apple|]1,Inf] There are %count% apples', 0],
  192. ['', '{0} There are no apples|{1}|]1,Inf] There are %count% apples', 1],
  193. // Indexed only tests which are Gettext PoFile* compatible strings.
  194. ['There are 0 apples', 'There is one apple|There are %count% apples', 0],
  195. ['There is one apple', 'There is one apple|There are %count% apples', 1],
  196. ['There are 2 apples', 'There is one apple|There are %count% apples', 2],
  197. // Tests for float numbers
  198. ['There is almost one apple', '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple', 0.7],
  199. ['There is one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1],
  200. ['There is more than one apple', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 1.7],
  201. ['There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0],
  202. ['There are no apples', '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0.0],
  203. ['There are no apples', '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple', 0],
  204. // Test texts with new-lines
  205. // with double-quotes and \n in id & double-quotes and actual newlines in text
  206. ["This is a text with a\n new-line in it. Selector = 0.", '{0}This is a text with a
  207. new-line in it. Selector = 0.|{1}This is a text with a
  208. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  209. new-line in it. Selector > 1.', 0],
  210. // with double-quotes and \n in id and single-quotes and actual newlines in text
  211. ["This is a text with a\n new-line in it. Selector = 1.", '{0}This is a text with a
  212. new-line in it. Selector = 0.|{1}This is a text with a
  213. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  214. new-line in it. Selector > 1.', 1],
  215. ["This is a text with a\n new-line in it. Selector > 1.", '{0}This is a text with a
  216. new-line in it. Selector = 0.|{1}This is a text with a
  217. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  218. new-line in it. Selector > 1.', 5],
  219. // with double-quotes and id split accros lines
  220. ['This is a text with a
  221. new-line in it. Selector = 1.', '{0}This is a text with a
  222. new-line in it. Selector = 0.|{1}This is a text with a
  223. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  224. new-line in it. Selector > 1.', 1],
  225. // with single-quotes and id split accros lines
  226. ['This is a text with a
  227. new-line in it. Selector > 1.', '{0}This is a text with a
  228. new-line in it. Selector = 0.|{1}This is a text with a
  229. new-line in it. Selector = 1.|[1,Inf]This is a text with a
  230. new-line in it. Selector > 1.', 5],
  231. // with single-quotes and \n in text
  232. ['This is a text with a\nnew-line in it. Selector = 0.', '{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.', 0],
  233. // with double-quotes and id split accros lines
  234. ["This is a text with a\nnew-line in it. Selector = 1.", "{0}This is a text with a\nnew-line in it. Selector = 0.|{1}This is a text with a\nnew-line in it. Selector = 1.|[1,Inf]This is a text with a\nnew-line in it. Selector > 1.", 1],
  235. // esacape pipe
  236. ['This is a text with | in it. Selector = 0.', '{0}This is a text with || in it. Selector = 0.|{1}This is a text with || in it. Selector = 1.', 0],
  237. // Empty plural set (2 plural forms) from a .PO file
  238. ['', '|', 1],
  239. // Empty plural set (3 plural forms) from a .PO file
  240. ['', '||', 1],
  241. // Floating values
  242. ['1.5 liters', '%count% liter|%count% liters', 1.5],
  243. ['1.5 litre', '%count% litre|%count% litres', 1.5, 'fr'],
  244. // Negative values
  245. ['-1 degree', '%count% degree|%count% degrees', -1],
  246. ['-1 degré', '%count% degré|%count% degrés', -1],
  247. ['-1.5 degrees', '%count% degree|%count% degrees', -1.5],
  248. ['-1.5 degré', '%count% degré|%count% degrés', -1.5, 'fr'],
  249. ['-2 degrees', '%count% degree|%count% degrees', -2],
  250. ['-2 degrés', '%count% degré|%count% degrés', -2],
  251. ];
  252. }
  253. /**
  254. * @dataProvider failingLangcodes
  255. */
  256. public function testFailedLangcodes($nplural, $langCodes)
  257. {
  258. $matrix = $this->generateTestData($langCodes);
  259. $this->validateMatrix($nplural, $matrix, false);
  260. }
  261. /**
  262. * @dataProvider successLangcodes
  263. */
  264. public function testLangcodes($nplural, $langCodes)
  265. {
  266. $matrix = $this->generateTestData($langCodes);
  267. $this->validateMatrix($nplural, $matrix);
  268. }
  269. /**
  270. * This array should contain all currently known langcodes.
  271. *
  272. * As it is impossible to have this ever complete we should try as hard as possible to have it almost complete.
  273. *
  274. * @return array
  275. */
  276. public function successLangcodes()
  277. {
  278. return [
  279. ['1', ['ay', 'bo', 'cgg', 'dz', 'id', 'ja', 'jbo', 'ka', 'kk', 'km', 'ko', 'ky']],
  280. ['2', ['nl', 'fr', 'en', 'de', 'de_GE', 'hy', 'hy_AM', 'en_US_POSIX']],
  281. ['3', ['be', 'bs', 'cs', 'hr']],
  282. ['4', ['cy', 'mt', 'sl']],
  283. ['6', ['ar']],
  284. ];
  285. }
  286. /**
  287. * This array should be at least empty within the near future.
  288. *
  289. * This both depends on a complete list trying to add above as understanding
  290. * the plural rules of the current failing languages.
  291. *
  292. * @return array with nplural together with langcodes
  293. */
  294. public function failingLangcodes()
  295. {
  296. return [
  297. ['1', ['fa']],
  298. ['2', ['jbo']],
  299. ['3', ['cbs']],
  300. ['4', ['gd', 'kw']],
  301. ['5', ['ga']],
  302. ];
  303. }
  304. /**
  305. * We validate only on the plural coverage. Thus the real rules is not tested.
  306. *
  307. * @param string $nplural Plural expected
  308. * @param array $matrix Containing langcodes and their plural index values
  309. * @param bool $expectSuccess
  310. */
  311. protected function validateMatrix($nplural, $matrix, $expectSuccess = true)
  312. {
  313. foreach ($matrix as $langCode => $data) {
  314. $indexes = array_flip($data);
  315. if ($expectSuccess) {
  316. $this->assertEquals($nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
  317. } else {
  318. $this->assertNotEquals((int) $nplural, \count($indexes), "Langcode '$langCode' has '$nplural' plural forms.");
  319. }
  320. }
  321. }
  322. protected function generateTestData($langCodes)
  323. {
  324. $translator = new class() {
  325. use TranslatorTrait {
  326. getPluralizationRule as public;
  327. }
  328. };
  329. $matrix = [];
  330. foreach ($langCodes as $langCode) {
  331. for ($count = 0; $count < 200; ++$count) {
  332. $plural = $translator->getPluralizationRule($count, $langCode);
  333. $matrix[$langCode][$count] = $plural;
  334. }
  335. }
  336. return $matrix;
  337. }
  338. }