Index: trunk/extensions/Translate/ffs/Gettext.php |
— | — | @@ -34,16 +34,20 @@ |
35 | 35 | } |
36 | 36 | $data = file_get_contents( $this->filename ); |
37 | 37 | $parse = GettextFFS::parseGettextData( $data ); |
38 | | - // Ugly ugly hack! part 1 |
39 | | - $parse['TEMPLATE']['HEADERS'] = $parse['HEADERS']; |
40 | 38 | return $parse['TEMPLATE']; |
41 | 39 | } |
42 | 40 | |
| 41 | + public function parseFileExt() { |
| 42 | + if ( $this->filename === false ) { |
| 43 | + return array(); |
| 44 | + } |
| 45 | + $data = file_get_contents( $this->filename ); |
| 46 | + return GettextFFS::parseGettextData( $data ); |
| 47 | + } |
43 | 48 | |
| 49 | + |
44 | 50 | public function parseMessages( StringMangler $mangler ) { |
45 | 51 | $defs = $this->parseFile(); |
46 | | - // Ugly ugly hack! part 2 |
47 | | - unset( $defs['HEADERS'] ); |
48 | 52 | $messages = array(); |
49 | 53 | foreach ( $defs as $key => $def ) { |
50 | 54 | if ( $this->pot ) { |
— | — | @@ -69,9 +73,7 @@ |
70 | 74 | if ( $reader instanceof GettextFormatReader ) { |
71 | 75 | $this->addAuthors( $reader->parseAuthors(), $code ); |
72 | 76 | $this->staticHeader = $reader->parseStaticHeader(); |
73 | | - $this->owndata = $reader->parseFile(); |
74 | | - // Ugly ugly hack! part 3 |
75 | | - $this->headers = $this->owndata['HEADERS']; |
| 77 | + $this->owndata = $reader->parseFileExt(); |
76 | 78 | } |
77 | 79 | if ( $readerEn instanceof GettextFormatReader ) { |
78 | 80 | $this->data = $readerEn->parseFile(); |
— | — | @@ -91,7 +93,7 @@ |
92 | 94 | $label = $this->group->getLabel(); |
93 | 95 | $languageName = TranslateUtils::getLanguageName( $code ); |
94 | 96 | |
95 | | - $headers = $this->headers; |
| 97 | + $headers = $this->owndata['HEADERS']; |
96 | 98 | $headers['Project-Id-Version'] = $label; |
97 | 99 | // TODO: make this customisable or something |
98 | 100 | // $headers['Report-Msgid-Bugs-To'] = $wgServer; |
— | — | @@ -161,8 +163,14 @@ |
162 | 164 | if ( isset( $this->data[$key]['ctxt'] ) ) { |
163 | 165 | $ckey = $this->data[$key]['ctxt']; |
164 | 166 | } |
165 | | - fwrite( $handle, $this->formatmsg( $m->definition(), $translation, $ckey ) ); |
166 | 167 | |
| 168 | + $pluralForms = false; |
| 169 | + if ( isset($this->owndata['METADATA']['plural']) ) { |
| 170 | + $pluralForms = $this->owndata['METADATA']['plural']; |
| 171 | + } |
| 172 | + |
| 173 | + fwrite( $handle, $this->formatmsg( $m->definition(), $translation, $ckey, $pluralForms ) ); |
| 174 | + |
167 | 175 | } |
168 | 176 | |
169 | 177 | return $out; |
— | — | @@ -203,7 +211,7 @@ |
204 | 212 | return implode( "\n", $output ) . "\n"; |
205 | 213 | } |
206 | 214 | |
207 | | - protected function formatmsg( $msgid, $msgstr, $msgctxt = false ) { |
| 215 | + protected function formatmsg( $msgid, $msgstr, $msgctxt = false, $pluralForms = false ) { |
208 | 216 | $output = array(); |
209 | 217 | |
210 | 218 | // FIXME: very ugly hack to allow gettext plurals to be exported. |
— | — | @@ -218,7 +226,7 @@ |
219 | 227 | $output[] = 'msgid ' . $this->escape( $forms[0] ); |
220 | 228 | $output[] = 'msgid_plural ' . $this->escape( $forms[1] ); |
221 | 229 | |
222 | | - $forms = $this->splitPlural( $msgstr, $this->plural[1] ); |
| 230 | + $forms = $this->splitPlural( $msgstr, $pluralForms ); |
223 | 231 | foreach( $forms as $index => $form ) { |
224 | 232 | $output[] = "msgstr[$index] " . $this->escape( $form ); |
225 | 233 | } |
— | — | @@ -318,15 +326,6 @@ |
319 | 327 | throw new MWException( "Gettext file header was not found:\n\n$data" ); |
320 | 328 | } |
321 | 329 | |
322 | | - /* At this stage we are only interested how many plurals forms we should |
323 | | - * be expecting when parsing the rest of this file. */ |
324 | | - $pluralCount = false; |
325 | | - if ( isset($headers['Plural-Forms']) ) { |
326 | | - if ( preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) ) { |
327 | | - $pluralCount = $matches[1]; |
328 | | - } |
329 | | - } |
330 | | - |
331 | 330 | // Extract some metadata from headers for easier use |
332 | 331 | $metadata = array(); |
333 | 332 | if ( isset($headers['X-Language-Code']) ) { |
— | — | @@ -337,6 +336,15 @@ |
338 | 337 | $metadata['group'] = $headers['X-Message-Group']; |
339 | 338 | } |
340 | 339 | |
| 340 | + /* At this stage we are only interested how many plurals forms we should |
| 341 | + * be expecting when parsing the rest of this file. */ |
| 342 | + $pluralCount = false; |
| 343 | + if ( isset($headers['Plural-Forms']) ) { |
| 344 | + if ( preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) ) { |
| 345 | + $pluralCount = $metadata['plural'] = $matches[1]; |
| 346 | + } |
| 347 | + } |
| 348 | + |
341 | 349 | // Then parse the messages |
342 | 350 | foreach ( $sections as $section ) { |
343 | 351 | if ( trim( $section ) === '' ) continue; |
— | — | @@ -380,7 +388,8 @@ |
381 | 389 | if ( $match !== null ) { |
382 | 390 | $actualForms[] = self::formatForWiki( $match ); |
383 | 391 | } else { |
384 | | - throw new MWException( "Plural $i not found, expecting total of $pluralCount" ); |
| 392 | + $actualForms[] = ''; |
| 393 | + error_log( "Plural $i not found, expecting total of $pluralCount for {$item['id']}" ); |
385 | 394 | } |
386 | 395 | } |
387 | 396 | |