TranslatorTest.php 16 KB

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