1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpMyAdmin\MoTranslator\Tests; |
6
|
|
|
|
7
|
|
|
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache; |
8
|
|
|
use PhpMyAdmin\MoTranslator\MoParser; |
9
|
|
|
use PhpMyAdmin\MoTranslator\Translator; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
use function chr; |
13
|
|
|
use function implode; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Test for gettext parsing. |
17
|
|
|
*/ |
18
|
|
|
class PluralTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Test for npgettext. |
22
|
|
|
* |
23
|
|
|
* @param int $number Number |
24
|
|
|
* @param string $expected Expected output |
25
|
|
|
* |
26
|
|
|
* @dataProvider providerTestNpgettext |
27
|
|
|
*/ |
28
|
|
|
public function testNpgettext(int $number, string $expected): void |
29
|
|
|
{ |
30
|
|
|
$parser = $this->getTranslator(''); |
31
|
|
|
$result = $parser->npgettext('context', "%d pig went to the market\n", "%d pigs went to the market\n", $number); |
32
|
|
|
$this->assertSame($expected, $result); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Data provider for test_npgettext. |
37
|
|
|
* |
38
|
|
|
* @return array[] |
39
|
|
|
*/ |
40
|
|
|
public static function providerTestNpgettext(): array |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
[ |
44
|
|
|
1, |
45
|
|
|
"%d pig went to the market\n", |
46
|
|
|
], |
47
|
|
|
[ |
48
|
|
|
2, |
49
|
|
|
"%d pigs went to the market\n", |
50
|
|
|
], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test for ngettext |
56
|
|
|
*/ |
57
|
|
|
public function testNgettext(): void |
58
|
|
|
{ |
59
|
|
|
$parser = $this->getTranslator(''); |
60
|
|
|
$translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]); |
61
|
|
|
$parser->setTranslation($translationKey, ''); |
62
|
|
|
$result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1); |
63
|
|
|
$this->assertSame('', $result); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array[] |
68
|
|
|
*/ |
69
|
|
|
public function dataProviderPluralForms(): array |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
|
|
['Plural-Forms: nplurals=2; plural=n != 1;'], |
73
|
|
|
['Plural-Forms: nplurals=1; plural=0;'], |
74
|
|
|
['Plural-Forms: nplurals=2; plural=(n > 1);'], |
75
|
|
|
[ |
76
|
|
|
'Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n' |
77
|
|
|
. '%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;', |
78
|
|
|
], |
79
|
|
|
['Plural-Forms: nplurals=2; plural=n >= 2 && (n < 11 || n > 99);'], |
80
|
|
|
['Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3;'], |
81
|
|
|
['Plural-Forms: nplurals=3; plural=n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2;'], |
82
|
|
|
[ |
83
|
|
|
'Plural-Forms: nplurals=2; plural=n != 1 && n != 2 && n != 3 &' |
84
|
|
|
. '& (n % 10 == 4 || n % 10 == 6 || n % 10 == 9);', |
85
|
|
|
], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test for ngettext |
91
|
|
|
* |
92
|
|
|
* @see https://github.com/phpmyadmin/motranslator/issues/37 |
93
|
|
|
* |
94
|
|
|
* @dataProvider dataProviderPluralForms |
95
|
|
|
*/ |
96
|
|
|
public function testNgettextSelectString(string $pluralForms): void |
97
|
|
|
{ |
98
|
|
|
$parser = $this->getTranslator(''); |
99
|
|
|
$parser->setTranslation( |
100
|
|
|
'', |
101
|
|
|
"Project-Id-Version: phpMyAdmin 5.1.0-dev\n" |
102
|
|
|
. "Report-Msgid-Bugs-To: [email protected]\n" |
103
|
|
|
. "PO-Revision-Date: 2020-09-01 09:12+0000\n" |
104
|
|
|
. "Last-Translator: William Desportes <[email protected]>\n" |
105
|
|
|
. 'Language-Team: English (United Kingdom) ' |
106
|
|
|
. "<https:\/\/hosted.weblate.org\/projects\/phpmyadmin\/master\/en_GB\/>\n" |
107
|
|
|
. "Language: en_GB\n" |
108
|
|
|
. "MIME-Version: 1.0\n" |
109
|
|
|
. "Content-Type: text\/plain; charset=UTF-8\n" |
110
|
|
|
. "Content-Transfer-Encoding: 8bit\n" |
111
|
|
|
. $pluralForms . "\n" |
112
|
|
|
. "X-Generator: Weblate 4.2.1-dev\n" |
113
|
|
|
. '' |
114
|
|
|
); |
115
|
|
|
$translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]); |
116
|
|
|
$parser->setTranslation($translationKey, 'ok'); |
117
|
|
|
$result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1); |
118
|
|
|
$this->assertSame('ok', $result); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function getTranslator(string $filename): Translator |
122
|
|
|
{ |
123
|
|
|
return new Translator(new InMemoryCache(new MoParser($filename))); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|