@@ -43,7 +43,7 @@ public function testPreviousTranslation(): void
43
43
msgctxt "ctx"
44
44
msgid "original"
45
45
msgid_plural "plural"
46
- msgstr "translation" ' ;
46
+ msgstr[0] "translation" ' ;
47
47
$ translations = $ this ->createPoLoader ()->loadString ($ po );
48
48
49
49
$ translation = $ translations ->find ('ctx ' , 'original ' );
@@ -56,7 +56,30 @@ public function testPreviousTranslation(): void
56
56
$ this ->assertEquals ($ translation ->getPreviousContext (), 'previous ctx ' );
57
57
$ this ->assertEquals ($ translation ->getPreviousOriginal (), 'previous original ' );
58
58
$ this ->assertEquals ($ translation ->getPreviousPlural (), 'previous plural ' );
59
+ }
60
+
61
+ public function testDisabledWithPreviousTranslation (): void
62
+ {
63
+ $ po = '#~ #| msgctxt "previous ctx"
64
+ #~ #| msgid "previous original"
65
+ #~ #| msgid_plural "previous plural"
66
+ #~ msgctxt "ctx"
67
+ #~ msgid "original"
68
+ #~ msgid_plural "plural"
69
+ #~ msgstr[0] "translation" ' ;
70
+ $ translations = $ this ->createPoLoader ()->loadString ($ po );
71
+
72
+ $ translation = $ translations ->find ('ctx ' , 'original ' );
73
+ $ this ->assertNotNull ($ translation );
74
+ $ this ->assertTrue ($ translation ->isDisabled ());
75
+ $ this ->assertEquals ($ translation ->getContext (), 'ctx ' );
76
+ $ this ->assertEquals ($ translation ->getOriginal (), 'original ' );
77
+ $ this ->assertEquals ($ translation ->getPlural (), 'plural ' );
78
+ $ this ->assertEquals ($ translation ->getTranslation (), 'translation ' );
59
79
80
+ $ this ->assertEquals ($ translation ->getPreviousContext (), 'previous ctx ' );
81
+ $ this ->assertEquals ($ translation ->getPreviousOriginal (), 'previous original ' );
82
+ $ this ->assertEquals ($ translation ->getPreviousPlural (), 'previous plural ' );
60
83
}
61
84
62
85
public function badFormattedPoProvider (): array
@@ -70,100 +93,119 @@ public function badFormattedPoProvider(): array
70
93
msgid"original"
71
94
msgstr"translation 2" ' ,
72
95
],
73
- 'msgstr before msgid ' => [
96
+ 'Out of order: msgstr before msgid ' => [
74
97
'/Expected msgid/ ' ,
75
98
'msgstr "translation"
76
99
msgid "original" ' ,
77
100
],
78
- 'Comments should come before the definitions ' => [
101
+ 'Out of order: msgctxt before msgid ' => [
102
+ '/Expected msgid/ ' ,
103
+ 'msgctxt "ctx"
104
+ msgstr "translation"
105
+ msgid "original" ' ,
106
+ ],
107
+ 'Out of order: Comment between the definitions ' => [
79
108
'/Expected msgstr/ ' ,
80
109
'msgid "original"
81
110
# Unexpected comment
82
111
msgstr "translation" ' ,
83
112
],
84
- 'msgid_plural requires an indexed msgstr ' => [
113
+ 'Out of order: Disabled translations (#~) cannot appear after previous translations (#|) ' => [
114
+ '/Inconsistent use of #~/ ' ,
115
+ '#|msgid "previous"
116
+ #~msgid "disabled"
117
+ #~msgstr "disabled translation"
118
+ msgid "original"
119
+ msgstr "translation" ' ,
120
+ ],
121
+ 'Out of order: msgctxt of a previous translation (#|) must appear before its msgid ' => [
122
+ '/Cannot redeclare the previous comment/ ' ,
123
+ '#|msgid "previous"
124
+ #|msgctxt "previous context"
125
+ #|msgid_plural "previous context"
126
+ msgid "original"
127
+ msgstr "translation" ' ,
128
+ ],
129
+ 'Indexed msgstr: msgid_plural requires an indexed msgstr ' => [
85
130
'/Expected character " \\["/ ' ,
86
131
'msgid "original"
87
132
msgid_plural "plural"
88
133
msgstr "translation" ' ,
89
134
],
90
- 'msgstr with a bad index ' => [
135
+ 'Indexed msgstr: After the index 0, the next should be 2 ' => [
91
136
'/The msgstr has an invalid index/ ' ,
92
137
'msgid "original"
93
138
msgid_plural "plural"
94
139
msgstr[0] "translation"
95
140
msgstr[2] "translation" ' ,
96
141
],
97
- 'msgstr with a bad index 2 ' => [
142
+ 'Indexed msgstr: Index has trash data (whitespace is ok) ' => [
98
143
'/Expected character "]"/ ' ,
99
144
'msgid "original"
100
145
msgid_plural "plural"
101
- msgstr[0 ] "translation"
146
+ msgstr[ 0 ] "translation"
102
147
msgstr[1s] "translation" ' ,
103
148
],
104
149
'Incomplete translation ' => [
105
150
'/Expected msgstr/ ' ,
106
151
'msgid "original" ' ,
107
152
],
108
- 'Bad quoted msgid ' => [
153
+ 'Incomplete disabled translation ' => [
154
+ '/Expected msgstr/ ' ,
155
+ '#~ msgid "original" ' ,
156
+ ],
157
+ 'Encoding: No quotes ' => [
109
158
'/Expected an opening quote/ ' ,
110
- 'msgid original
111
- msgstr " translation" ' ,
159
+ 'msgid " original"
160
+ msgstr translation ' ,
112
161
],
113
- 'Unquoted newline ' => [
114
- '/Newline character must be escaped / ' ,
162
+ 'Encoding: Missing opening quote ' => [
163
+ '/Expected an opening quote / ' ,
115
164
'msgid "original"
116
- msgstr "trans
117
- lation" ' ,
165
+ msgstr translation" ' ,
118
166
],
119
- 'Bad escaped octal ' => [
167
+ 'Encoding: Missing closing quote ' => [
168
+ '/Expected a closing quot/ ' ,
169
+ 'msgid "original"
170
+ msgstr "translation ' ,
171
+ ],
172
+ 'Encoding: Unescaped newline (using \\n) ' => [
173
+ '/Newline character must be escaped/ ' ,
174
+ "msgid \"original \"
175
+ msgstr \"trans \nlation \"" ,
176
+ ],
177
+ 'Encoding: Unescaped newline (using \\r) ' => [
178
+ '/Newline character must be escaped/ ' ,
179
+ "msgid \"original \"
180
+ msgstr \"trans \rlation \"" ,
181
+ ],
182
+ 'Encoding: Invalid octal digit ' => [
120
183
'/Invalid quoted character/ ' ,
121
184
'msgid "original"
122
185
msgstr "translation\8" ' ,
123
186
],
124
- 'Out of range octal ' => [
187
+ 'Encoding: Octal out of range ' => [
125
188
'/Octal value out of range/ ' ,
126
189
'msgid "original"
127
190
msgstr "translation\777" ' ,
128
191
],
129
- 'Bad escaped hex ' => [
192
+ 'Encoding: Invalid hexadecimal digit ' => [
130
193
'/Expected at least one occurrence of hexadecimal/ ' ,
131
194
'msgid "original"
132
195
msgstr "translation\xGG" ' ,
133
196
],
134
- 'Bad escaped hex ' => [
135
- '/Expected at least one occurrence of hexadecimal/ ' ,
136
- 'msgid "original"
137
- msgstr "translation\xGG" ' ,
138
- ],
139
- 'Bad escaped unicode ' => [
197
+ 'Encoding: Invalid unicode digit ' => [
140
198
'/Expected at least one occurrence of hexadecimal/ ' ,
141
199
'msgid "original"
142
200
msgstr "translation\uZZ" ' ,
143
201
],
144
- 'Disabled translations (#~) cannot appear after previous translations (#|) ' => [
145
- '/Inconsistent use of #~/ ' ,
146
- '#|msgid "previous"
147
- #~msgid "disabled"
148
- #~msgstr "disabled translation"
149
- msgid "original"
150
- msgstr "translation" ' ,
151
- ],
152
- 'Invalid identifier ' => [
202
+ 'Invalid identifier "unknown" ' => [
153
203
'/Expected msgid/ ' ,
154
204
'unknown "original"
155
205
msgstr "translation" ' ,
156
206
],
157
- 'msgctxt of a previous translation must come before its msgid ' => [
158
- '/Cannot redeclare the previous comment/ ' ,
159
- '#|msgid "previous"
160
- #|msgctxt "previous context"
161
- #|msgid_plural "previous context"
162
- msgid "original"
163
- msgstr "translation" ' ,
164
- ],
165
207
// The checks below depends on the $throwOnWarning = true
166
- 'msgid, msgid_plural and msgstr cannot begin nor end with newline ' => [
208
+ 'msgid, msgid_plural and msgstr cannot begin nor end with a newline ' => [
167
209
'/msgstr cannot start nor end with a newline/ ' ,
168
210
'msgid "original"
169
211
msgstr "translation\n" ' ,
@@ -195,7 +237,7 @@ public function badFormattedPoProvider(): array
195
237
true ,
196
238
],
197
239
'Two plural forms with just one plural translation ' => [
198
- '/The translation doesn \' t have all the \\d+ plural forms / ' ,
240
+ '/The translation has \\ d+ plural forms, while the header expects \\d+/ ' ,
199
241
'msgid ""
200
242
msgstr "Language: en_US\n"
201
243
"Content-Type: text/plain; charset=UTF-8\n"
@@ -206,6 +248,28 @@ public function badFormattedPoProvider(): array
206
248
msgstr[0] "translation" ' ,
207
249
true ,
208
250
],
251
+ 'Two plural forms with 3 plural translations ' => [
252
+ '/The translation has \\d+ plural forms, while the header expects \\d+/ ' ,
253
+ 'msgid ""
254
+ msgstr "Language: en_US\n"
255
+ "Content-Type: text/plain; charset=UTF-8\n"
256
+ "Plural-Forms: nplurals=2; plural=n != 1;\n"
257
+
258
+ msgid "original"
259
+ msgid_plural "plural"
260
+ msgstr[0] "translation"
261
+ msgstr[1] "translation"
262
+ msgstr[2] "translation" ' ,
263
+ true ,
264
+ ],
265
+ 'Dangling comment in the end of the data ' => [
266
+ '/Comment ignored at the end/ ' ,
267
+ 'msgid "original"
268
+ msgstr "translation"
269
+
270
+ # Dangling comment ' ,
271
+ true ,
272
+ ],
209
273
];
210
274
}
211
275
@@ -215,6 +279,11 @@ public function badFormattedPoProvider(): array
215
279
public function testBadFormattedPo (string $ exceptionPattern , string $ po , bool $ throwOnWarning = false ): void
216
280
{
217
281
$ this ->expectExceptionMessageMatches ($ exceptionPattern );
218
- $ this ->createPoLoader ()->loadString ($ po , null , $ throwOnWarning );
282
+ $ loader = $ this ->createPoLoader ();
283
+ if ($ throwOnWarning ) {
284
+ $ loader ->loadStringExtended ($ po , null , $ throwOnWarning );
285
+ } else {
286
+ $ loader ->loadString ($ po );
287
+ }
219
288
}
220
289
}
0 commit comments