r73241 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73240‎ | r73241 | r73242 >
Date:19:05, 17 September 2010
Author:siebrand
Status:deferred
Tags:
Comment:
Remove all traces of old style gettext support implementation.
Modified paths:
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/ffs/Gettext.php (modified) (history)
  • /trunk/extensions/Translate/groups/StatusNet/README (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/MessageGroups.php
@@ -637,85 +637,6 @@
638638 }
639639
640640 /**
641 - * This implements the old style message group for Gettext format.
642 - * @todo Move to the new interface.
643 - */
644 -class GettextMessageGroup extends MessageGroupOld {
645 - protected $type = 'gettext';
646 -
647 - /**
648 - * Name of the array where all messages are stored, if applicable.
649 - */
650 - protected $potFile = 'messages';
651 - public function getPotFile() { return $this->potFile; }
652 - public function setPotFile( $value ) { $this->potFile = $value; }
653 -
654 - protected $codeMap = array();
655 - public function setCodeMap( $map ) {
656 - $this->codeMap = $map;
657 - }
658 -
659 - protected $path = '';
660 - public function getPath() { return $this->path; }
661 - public function setPath( $value ) { $this->path = $value; }
662 -
663 - protected $prefix = '';
664 - public function getPrefix() { return $this->prefix; }
665 - public function setPrefix( $value ) { $this->prefix = $value; }
666 -
667 - public $filePattern = '%CODE%.po';
668 -
669 - public function getMessageFile( $code ) {
670 - if ( $code == 'en' ) {
671 - return $this->getPotFile();
672 - } else {
673 - $origCode = $code;
674 -
675 - if ( isset( $this->codeMap[$code] ) ) {
676 - $code = $this->codeMap[$code];
677 - }
678 -
679 - /** If this (valid) code is a mapped target, do not provide a file.
680 - * Example: 'no' => 'nb'.
681 - */
682 - $mappedCodes = array_values( $this->codeMap );
683 - if ( $code == $origCode && in_array( $code, $mappedCodes ) ) {
684 - return '';
685 - }
686 -
687 - return $this->replaceVariables( $this->filePattern, $code );
688 - }
689 - }
690 -
691 - public function replaceVariables( $string, $code ) {
692 - return str_replace( '%CODE%', $code, $string );
693 - }
694 -
695 - public static function factory( $label, $id ) {
696 - $group = new GettextMessageGroup;
697 - $group->setLabel( $label );
698 - $group->setId( $id );
699 -
700 - return $group;
701 - }
702 -
703 - public function getReader( $code ) {
704 - $reader = new GettextFormatReader( $this->getMessageFileWithPath( $code ) );
705 - $reader->setPrefix( $this->prefix );
706 -
707 - if ( $code === 'en' ) {
708 - $reader->setPotMode( true );
709 - }
710 -
711 - return $reader;
712 - }
713 -
714 - public function getWriter() {
715 - return new GettextFormatWriter( $this );
716 - }
717 -}
718 -
719 -/**
720641 * Group for messages that can be controlled via a page in %MediaWiki namespace.
721642 *
722643 * In the page comments start with # and continue till the end of the line.
Index: trunk/extensions/Translate/groups/StatusNet/README
@@ -7,34 +7,5 @@
88
99 Settings:
1010 wfAddNamespace( 1208, 'StatusNet' );
11 -
12 -As long as new message groups have not yet been implemented for gettext, the
13 -following settings are needed:
14 -
15 -$wgHooks['TranslatePostInitGroups'][] = array( 'setupStatusnet' );
16 -
17 -function setupStatusnet( &$cc ) {
18 - global $wgTranslateGroupRoot;
19 - global $IP;
20 - $id = 'out-statusnet';
21 - $mg = GettextMessageGroup::factory( 'StatusNet', $id );
22 - $mg->setPotFile( 'locale/statusnet.pot' );
23 - $mg->setPath( "$wgTranslateGroupRoot/statusnet/" );
24 - $mg->filePattern = "locale/%CODE%/LC_MESSAGES/statusnet.po";
25 - $mg->setDescription( "{{int:bw-desc-statusnet}}" );
26 - $mg->namespaces = array( NS_STATUSNET, NS_STATUSNET_TALK );
27 - $mg->setCodeMap( array(
28 - 'en-gb' => 'en_GB',
29 - 'no' => 'nb',
30 - 'pt-br' => 'pt_BR',
31 - 'zh-hans' => 'zh_CN',
32 - 'zh-hant' => 'zh_TW'
33 - ) );
34 - $cc[$id] = $mg;
35 - return true;
36 -}
37 -
38 -When GettextMessageGroupFFS has been implemented, the following will suffice:
3911 $wgTranslateGroupFiles[] = "$IP/extensions/Translate/groups/StatusNet/StatusNet.yml";
40 -
41 -Add more to $wgTranslateGroupFiles for supported plugins.
 12+$wgTranslateGroupFiles[] = "$IP/extensions/Translate/groups/StatusNet/StatusNet-plugins.yml";
Index: trunk/extensions/Translate/_autoload.php
@@ -80,8 +80,6 @@
8181 $wgAutoloadClasses['WikiFormatWriter'] = $dir . 'ffs/Wiki.php';
8282 $wgAutoloadClasses['WikiExtensionFormatReader'] = $dir . 'ffs/WikiExtension.php';
8383 $wgAutoloadClasses['WikiExtensionFormatWriter'] = $dir . 'ffs/WikiExtension.php';
84 -$wgAutoloadClasses['GettextFormatReader'] = $dir . 'ffs/Gettext.php';
85 -$wgAutoloadClasses['GettextFormatWriter'] = $dir . 'ffs/Gettext.php';
8684 $wgAutoloadClasses['XliffFormatWriter'] = $dir . 'ffs/Xliff.php';
8785 /**@}*/
8886
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -682,21 +682,6 @@
683683 }
684684
685685 protected function formatGettextComments() {
686 - if ( $this->group instanceof GettextMessageGroup ) {
687 - $reader = $this->group->getReader( 'en' );
688 - if ( $reader ) {
689 - global $wgContLang;
690 -
691 - $mykey = $wgContLang->lcfirst( $this->page );
692 - $data = $reader->parseFile();
693 - $help = trim( GettextFormatWriter::formatComments( @$data[$mykey]['comments'], false, @$data[$mykey]['flags'] ) );
694 - // Do not display an empty comment. That's no help and takes up unnecessary space.
695 - if ( $help !== '#:' ) {
696 - return "<hr /><pre>$help</pre>";
697 - }
698 - }
699 - }
700 -
701686 if ( $this->group instanceof FileBasedMessageGroup ) {
702687 $ffs = $this->group->getFFS();
703688 if ( $ffs instanceof GettextFFS ) {
Index: trunk/extensions/Translate/ffs/Gettext.php
@@ -15,348 +15,6 @@
1616 class GettextPluralException extends MwException {}
1717
1818 /**
19 - * Old-style parser for gettext file format.
20 - */
21 -class GettextFormatReader extends SimpleFormatReader {
22 - protected $pot = false;
23 -
24 - public function setPotMode( $value ) {
25 - $this->pot = $value;
26 - }
27 -
28 - protected $prefix = '';
29 -
30 - public function setPrefix( $value ) {
31 - $this->prefix = $value;
32 - }
33 -
34 - /**
35 - * Get authors from gettext file.
36 - *
37 - * @todo Implement this.
38 - * return \array List of authors
39 - */
40 - public function parseAuthors() {
41 - return array();
42 - }
43 -
44 - /**
45 - * Returns static header of gettext file.
46 - * Static part recognition is based on finding "# --" as beginning and
47 - * "msgid" as end.
48 - * return /string Static header of gettext file.
49 - */
50 - public function parseStaticHeader() {
51 - if ( $this->filename === false ) {
52 - return '';
53 - }
54 -
55 - $data = file_get_contents( $this->filename );
56 - $start = (int) strpos( $data, '# --' );
57 -
58 - if ( $start ) {
59 - $start += 5;
60 - }
61 -
62 - $end = (int) strpos( $data, "msgid" );
63 -
64 - return substr( $data, $start, $end - $start );
65 - }
66 -
67 - public function parseFile() {
68 - if ( $this->filename === false ) {
69 - return array();
70 - }
71 -
72 - $data = file_get_contents( $this->filename );
73 - $parse = GettextFFS::parseGettextData( $data );
74 -
75 - return $parse['TEMPLATE'];
76 - }
77 -
78 - public function parseFileExt() {
79 - if ( $this->filename === false ) {
80 - return array();
81 - }
82 -
83 - $data = file_get_contents( $this->filename );
84 -
85 - return GettextFFS::parseGettextData( $data );
86 - }
87 -
88 - public function parseMessages( StringMangler $mangler ) {
89 - $defs = $this->parseFile();
90 - $messages = array();
91 -
92 - foreach ( $defs as $key => $def ) {
93 - if ( $this->pot ) {
94 - $messages[$key] = $def['id'];
95 - } else {
96 - if ( $def['str'] !== '' ) {
97 - $messages[$key] = $def['str'];
98 - }
99 - }
100 - }
101 -
102 - return $mangler->mangle( $messages );
103 - }
104 -}
105 -
106 -/**
107 - * Old-style writer for gettext file format.
108 - */
109 -class GettextFormatWriter extends SimpleFormatWriter {
110 - protected $data = array();
111 - protected $plural = array( false, 0 );
112 -
113 - public function load( $code ) {
114 - $reader = $this->group->getReader( $code );
115 - $readerEn = $this->group->getReader( 'en' );
116 -
117 - if ( $reader instanceof GettextFormatReader ) {
118 - $this->addAuthors( $reader->parseAuthors(), $code );
119 - $this->staticHeader = $reader->parseStaticHeader();
120 - $this->owndata = $reader->parseFileExt();
121 - }
122 -
123 - if ( $readerEn instanceof GettextFormatReader ) {
124 - $this->data = $readerEn->parseFile();
125 - }
126 - }
127 -
128 - public function exportLanguage( $handle, MessageCollection $messages ) {
129 - global $wgSitename, $wgServer, $wgTranslateDocumentationLanguageCode;
130 -
131 - $code = $messages->code;
132 - $this->load( $code );
133 - $lang = Language::factory( 'en' );
134 -
135 - $out = '';
136 - $now = wfTimestampNow();
137 - $label = $this->group->getLabel();
138 - $languageName = TranslateUtils::getLanguageName( $code );
139 -
140 - $headers = isset( $this->owndata['HEADERS'] ) ? $this->owndata['HEADERS'] : array();
141 - $headers['Project-Id-Version'] = $label;
142 -
143 - /**
144 - * @todo Make this customisable or something
145 - * $headers['Report-Msgid-Bugs-To'] = $wgServer;
146 - */
147 -
148 - /** @todo sprintfDate doesn't support any time zone flags
149 - * $headers['POT-Creation-Date']
150 - */
151 -
152 - $headers['PO-Revision-Date'] = $lang->sprintfDate( 'xnY-xnm-xnd xnH:xni:xns+0000', $now );
153 -
154 - /**
155 - * @todo Link to portal pages?
156 - * $headers['Language-Team'] = $languageName;
157 - */
158 -
159 - $headers['Content-Type'] = 'text/plain; charset=UTF-8';
160 - $headers['Content-Transfer-Encoding'] = '8bit';
161 -
162 - $headers['X-Generator'] = 'MediaWiki ' . SpecialVersion::getVersion() .
163 - "; Translate extension (" . TRANSLATE_VERSION . ")";
164 -
165 - $headers['X-Translation-Project'] = "$wgSitename at $wgServer";
166 - $headers['X-Language-Code'] = $code;
167 - $headers['X-Message-Group'] = $this->group->getId();
168 -
169 - $headerlines = array( '' );
170 - foreach ( $headers as $key => $value ) {
171 - $headerlines[] = "$key: $value\n";
172 - }
173 -
174 - fwrite( $handle, "# Translation of $label to $languageName\n#\n" );
175 - fwrite( $handle, $this->formatAuthors( "# Author@$wgSitename: ", $code ) );
176 - fwrite( $handle, "# --\n" );
177 -
178 - $header = preg_replace( '/^# translation of (.*) to (.*)$\n/im', '', $this->staticHeader );
179 -
180 - fwrite( $handle, $header );
181 - fwrite( $handle, $this->formatmsg( '', $headerlines ) );
182 -
183 - $mangler = $this->group->getMangler();
184 -
185 - foreach ( $messages as $key => $m ) {
186 - $flags = array();
187 -
188 - $key = $mangler->unmangle( $key );
189 -
190 - $translation = $m->translation();
191 - # CASE2: no translation
192 - if ( $translation === null ) {
193 - $translation = '';
194 - }
195 -
196 - # CASE3: optional messages; accept only if different
197 - if ( $m->hasTag( 'optional' ) ) {
198 - $flags[] = 'x-optional';
199 - }
200 -
201 - # Remove explicit fuzzy markings from the translation before export
202 - $flags = array();
203 - $comments = array();
204 - if ( isset( $this->data[$key]['flags'] ) ) {
205 - $flags = $this->data[$key]['flags'];
206 - }
207 -
208 - if ( strpos( $translation, TRANSLATE_FUZZY ) !== false ) {
209 - $translation = str_replace( TRANSLATE_FUZZY, '', $translation );
210 - $flags[] = 'fuzzy';
211 - }
212 -
213 - $documentation = '';
214 - if ( $wgTranslateDocumentationLanguageCode ) {
215 - $documentation = TranslateUtils::getMessageContent( $key, $wgTranslateDocumentationLanguageCode );
216 - }
217 -
218 - $comments = array();
219 - if ( isset( $this->data[$key]['comments'] ) ) {
220 - $comments = $this->data[$key]['comments'];
221 - }
222 -
223 - fwrite( $handle, self::formatComments( $comments, $documentation, $flags ) );
224 -
225 - $ckey = '';
226 - if ( isset( $this->data[$key]['ctxt'] ) ) {
227 - $ckey = $this->data[$key]['ctxt'];
228 - }
229 -
230 - $pluralForms = false;
231 - if ( isset( $this->owndata['METADATA']['plural'] ) ) {
232 - $pluralForms = $this->owndata['METADATA']['plural'];
233 - }
234 -
235 - fwrite( $handle, $this->formatmsg( $m->definition(), $translation, $ckey, $pluralForms ) );
236 - }
237 -
238 - return $out;
239 - }
240 -
241 - protected function escape( $line ) {
242 - // There may be \ as a last character, for keeping trailing whitespace
243 - $line = preg_replace( '/\\\\$/', '', $line );
244 - $line = addcslashes( $line, '\\"' );
245 - $line = str_replace( "\n", '\n', $line );
246 - $line = '"' . $line . '"';
247 -
248 - return $line;
249 - }
250 -
251 - public static function formatComments( $comments, $documentation = false, $flags = false ) {
252 - if ( $documentation ) {
253 - foreach ( explode( "\n", $documentation ) as $line ) {
254 - $comments['.'][] = $line;
255 - }
256 - }
257 -
258 - if ( $flags ) {
259 - $comments[','][] = implode( ', ', $flags );
260 - }
261 -
262 - // Ensure there is always something
263 - if ( !count( $comments ) ) {
264 - $comments[':'][] = '';
265 - }
266 -
267 - $order = array( '', '.', ':', ',', '|' );
268 - $output = array();
269 - foreach ( $order as $type ) {
270 - if ( !isset( $comments[$type] ) ) {
271 - continue;
272 - }
273 -
274 - foreach ( $comments[$type] as $value ) {
275 - $output[] = "#$type $value";
276 - }
277 - }
278 -
279 - return implode( "\n", $output ) . "\n";
280 - }
281 -
282 - protected function formatmsg( $msgid, $msgstr, $msgctxt = false, $pluralForms = false ) {
283 - $output = array();
284 -
285 - // @todo Very ugly hack to allow gettext plurals to be exported.
286 - if ( $msgstr == '{{PLURAL:GETTEXT|}}' ) {
287 - return '';
288 - }
289 -
290 - if ( $msgctxt ) {
291 - $output[] = 'msgctxt ' . $this->escape( $msgctxt );
292 - }
293 -
294 - if ( preg_match( '/{{PLURAL:GETTEXT/i', $msgid ) ) {
295 - $forms = $this->splitPlural( $msgid, 2 );
296 - $output[] = 'msgid ' . $this->escape( $forms[0] );
297 - $output[] = 'msgid_plural ' . $this->escape( $forms[1] );
298 -
299 - try {
300 - $forms = $this->splitPlural( $msgstr, $pluralForms );
301 - foreach ( $forms as $index => $form ) {
302 - $output[] = "msgstr[$index] " . $this->escape( $form );
303 - }
304 - } catch ( GettextPluralException $e ) {
305 - $output[] = "# Plural problem";
306 - }
307 - } else {
308 - $output[] = 'msgid ' . $this->escape( $msgid );
309 -
310 - // Special case for the header
311 - if ( is_array( $msgstr ) ) {
312 - $output[] = 'msgstr ""';
313 - foreach ( $msgstr as $line )
314 - $output[] = $this->escape( $line );
315 - } else {
316 - $output[] = 'msgstr ' . $this->escape( $msgstr );
317 - }
318 - }
319 -
320 - $out = implode( "\n", $output ) . "\n\n";
321 -
322 - return $out;
323 -
324 - }
325 -
326 - protected function splitPlural( $text, $forms ) {
327 - if ( $forms === 1 ) {
328 - return $text;
329 - } elseif ( !$forms ) {
330 - $forms = (int) $forms;
331 - throw new GettextPluralException( "Don't know how to split $text into $forms forms" );
332 - }
333 -
334 - $splitPlurals = array();
335 - for ( $i = 0; $i < $forms; $i++ ) {
336 - $plurals = array();
337 - $match = preg_match_all( '/{{PLURAL:GETTEXT\|(.*)}}/iU', $text, $plurals );
338 -
339 - if ( !$match ) {
340 - throw new GettextPluralException( "Failed to parse plural for: $text" );
341 - }
342 -
343 - $pluralForm = $text;
344 - foreach ( $plurals[0] as $index => $definition ) {
345 - $parsedFormsArray = explode( '|', $plurals[1][$index] );
346 - if ( !isset( $parsedFormsArray[$i] ) ) {
347 - error_log( "Too few plural forms in: $text" );
348 - $pluralForm = '';
349 - } else {
350 - $pluralForm = str_replace( $pluralForm, $definition, $parsedFormsArray[$i] );
351 - }
352 - }
353 - $splitPlurals[$i] = $pluralForm;
354 - }
355 -
356 - return $splitPlurals;
357 - }
358 -}
359 -
360 -/**
36119 * New-style FFS class that implements support for gettext file format.
36220 * @ingroup FFS
36321 */

Status & tagging log