TranslatorTest.php 14 KB

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