TranslatorTest.php 15 KB

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