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