forked from php-gettext/Gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePoLoaderTestCase.php
More file actions
289 lines (258 loc) · 12.3 KB
/
BasePoLoaderTestCase.php
File metadata and controls
289 lines (258 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php
declare(strict_types = 1);
namespace Gettext\Tests;
use Gettext\Loader\Loader;
use Gettext\Translation;
use PHPUnit\Framework\TestCase;
abstract class BasePoLoaderTestCase extends TestCase
{
abstract protected function createPoLoader(): Loader;
public function testPoLoader(): void
{
$loader = $this->createPoLoader();
$translations = $loader->loadFile(__DIR__.'/assets/translations.po');
$description = $translations->getDescription();
$this->assertSame(
<<<'EOT'
SOME DESCRIPTIVE TITLE
Copyright (C) YEAR Free Software Foundation, Inc.
This file is distributed under the same license as the PACKAGE package.
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
EOT
,
$description
);
$this->assertSame(['fuzzy'], $translations->getFlags()->toArray());
$this->assertCount(14, $translations);
$array = $translations->getTranslations();
$this->translation1(array_shift($array));
$this->translation2(array_shift($array));
$this->translation3(array_shift($array));
$this->translation4(array_shift($array));
$this->translation5(array_shift($array));
$this->translation6(array_shift($array));
$this->translation7(array_shift($array));
$this->translation8(array_shift($array));
$this->translation9(array_shift($array));
$this->translation10(array_shift($array));
$this->translation11(array_shift($array));
$this->translation12(array_shift($array));
$this->translation13(array_shift($array));
$this->translation14(array_shift($array));
$headers = $translations->getHeaders()->toArray();
$this->assertCount(12, $headers);
$this->assertSame('text/plain; charset=UTF-8', $headers['Content-Type']);
$this->assertSame('8bit', $headers['Content-Transfer-Encoding']);
$this->assertSame('', $headers['POT-Creation-Date']);
$this->assertSame('', $headers['PO-Revision-Date']);
$this->assertSame('', $headers['Last-Translator']);
$this->assertSame('', $headers['Language-Team']);
$this->assertSame('1.0', $headers['MIME-Version']);
$this->assertSame('bs', $headers['Language']);
$this->assertSame(
'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);',
$headers['Plural-Forms']
);
$this->assertSame('Poedit 1.6.5', $headers['X-Generator']);
$this->assertSame('gettext generator test', $headers['Project-Id-Version']);
$this->assertSame('testingdomain', $headers['X-Domain']);
$this->assertSame('testingdomain', $translations->getDomain());
$this->assertSame('bs', $translations->getLanguage());
}
private function translation1(Translation $translation): void
{
$this->assertSame(
'Ensure this value has at least %(limit_value)d character (it has %sd).',
$translation->getOriginal()
);
$this->assertSame(
'Ensure this value has at least %(limit_value)d characters (it has %sd).',
$translation->getPlural()
);
$this->assertSame('', $translation->getTranslation());
$this->assertSame(['', ''], $translation->getPluralTranslations());
}
private function translation2(Translation $translation): void
{
$this->assertSame(
'Ensure this value has at most %(limit_value)d character (it has %sd).',
$translation->getOriginal()
);
$this->assertSame(
'Ensure this value has at most %(limit_value)d characters (it has %sd).',
$translation->getPlural()
);
$this->assertSame('', $translation->getTranslation());
$this->assertSame(['', ''], $translation->getPluralTranslations());
}
private function translation3(Translation $translation): void
{
$this->assertSame('%ss must be unique for %ss %ss.', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('%ss mora da bude jedinstven za %ss %ss.', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
}
private function translation4(Translation $translation): void
{
$this->assertSame('and', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('i', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertSame(['c-format'], $translation->getFlags()->toArray());
}
private function translation5(Translation $translation): void
{
$this->assertSame('Value %sr is not a valid choice.', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertSame(['This is a extracted comment'], $translation->getExtractedComments()->toArray());
}
private function translation6(Translation $translation): void
{
$this->assertSame('This field cannot be null.', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('Ovo polje ne može ostati prazno.', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(1, $translation->getReferences());
$this->assertSame(['C:/Users/Me/Documents/foo2.php' => [1]], $translation->getReferences()->toArray());
}
private function translation7(Translation $translation): void
{
$this->assertSame('This field cannot be blank.', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('Ovo polje ne može biti prazno.', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(1, $translation->getReferences());
$this->assertSame(['C:/Users/Me/Documents/foo1.php' => []], $translation->getReferences()->toArray());
}
private function translation8(Translation $translation): void
{
$this->assertSame('Field of type: %ss', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('Polje tipa: %ss', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(2, $translation->getReferences());
$this->assertSame(
[
'attributes/address/composer.php' => [8],
'attributes/address/form.php' => [7],
],
$translation->getReferences()->toArray()
);
}
private function translation9(Translation $translation): void
{
$this->assertSame('Integer', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('Cijeo broj', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(0, $translation->getReferences());
$this->assertCount(1, $translation->getComments());
$this->assertSame(['a simple line comment is above'], $translation->getComments()->toArray());
}
private function translation10(Translation $translation): void
{
$this->assertSame('{test1}', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame("test1\n<div>\n test2\n</div>\ntest3", $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(0, $translation->getComments());
$this->assertCount(3, $translation->getReferences());
$this->assertSame(
[
'/var/www/test/test.php' => [96, 97],
'/var/www/test/test2.php' => [98],
],
$translation->getReferences()->toArray()
);
}
private function translation11(Translation $translation): void
{
$this->assertSame('{test2}', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame("test1\n<div>\n test2\n</div>\ntest3", $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(0, $translation->getComments());
$this->assertCount(1, $translation->getReferences());
$this->assertSame(
['/var/www/test/test.php' => [96]],
$translation->getReferences()->toArray()
);
}
private function translation12(Translation $translation): void
{
$this->assertSame('Multibyte test', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame('日本人は日本で話される言語です!', $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(0, $translation->getComments());
$this->assertCount(0, $translation->getReferences());
}
private function translation13(Translation $translation): void
{
$this->assertSame('Tabulation test', $translation->getOriginal());
$this->assertNull($translation->getPlural());
$this->assertSame("FIELD\tFIELD", $translation->getTranslation());
$this->assertCount(0, $translation->getPluralTranslations());
$this->assertCount(0, $translation->getComments());
$this->assertCount(0, $translation->getReferences());
}
private function translation14(Translation $translation): void
{
$this->assertSame('%s has been added to your cart.', $translation->getOriginal());
$this->assertSame('%s have been added to your cart.', $translation->getPlural());
$this->assertSame('%s has been added to your cart.', $translation->getTranslation());
$this->assertSame(['%s have been added to your cart.'], $translation->getPluralTranslations());
$this->assertCount(1, $translation->getComments());
$this->assertCount(0, $translation->getReferences());
}
public function stringDecodeProvider()
{
return [
['"test"', 'test'],
['"\'test\'"', "'test'"],
['"Special chars: \\n \\t \\\\ "', "Special chars: \n \t \\ "],
['"Newline\nSlash and n\\\\nend"', "Newline\nSlash and n\\nend"],
['"Quoted \\"string\\" with %s"', 'Quoted "string" with %s'],
['"\\\\x07 - aka \\\\a: \\a"', "\\x07 - aka \\a: \x07"],
['"\\\\x08 - aka \\\\b: \\b"', "\\x08 - aka \\b: \x08"],
['"\\\\x09 - aka \\\\t: \\t"', "\\x09 - aka \\t: \t"],
['"\\\\x0a - aka \\\\n: \\n "', "\\x0a - aka \\n: \n "],
['"\\\\x0b - aka \\\\v: \\v"', "\\x0b - aka \\v: \x0b"],
['"\\\\x0c - aka \\\\f: \\f"', "\\x0c - aka \\f: \x0c"],
['"\\\\x0d - aka \\\\r: \\r "', "\\x0d - aka \\r: \r "],
['"\\\\x22 - aka \\": \\""', '\x22 - aka ": "'],
['"\\\\x5c - aka \\\\: \\\\"', '\\x5c - aka \\: \\'],
];
}
/**
* @dataProvider stringDecodeProvider
*/
public function testStringDecode(string $source, string $decoded): void
{
$po = <<<EOT
msgid "source"
msgstr {$source}
EOT;
$translations = $this->createPoLoader()->loadString($po);
$this->assertSame($decoded, $translations->find(null, 'source')->getTranslation());
}
public function testMultilineDisabledTranslations(): void
{
$po = <<<'EOT'
#~ msgid "Last agent hours-description"
#~ msgstr ""
#~ "How many hours in the past can system look at finding the last agent? "
#~ "This parameter is only used if 'Call Last Agent' is set to 'YES'."
EOT;
$loader = $this->createPoLoader();
$translations = $loader->loadString($po);
$translation = $translations->find(null, 'Last agent hours-description');
$this->assertTrue($translation->isDisabled());
$this->assertEquals(
"How many hours in the past can system look at finding the last agent? This parameter is only used if 'Call Last Agent' is set to 'YES'.",
$translation->getTranslation()
);
}
}