http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72845

Revision: 72845
Author:   nikerabbit
Date:     2010-09-12 09:40:47 +0000 (Sun, 12 Sep 2010)

Log Message:
-----------
Fixed Gettext messages parsing and implemented header output for it

Modified Paths:
--------------
    trunk/extensions/Translate/ffs/Gettext.php

Modified: trunk/extensions/Translate/ffs/Gettext.php
===================================================================
--- trunk/extensions/Translate/ffs/Gettext.php  2010-09-12 09:32:21 UTC (rev 
72844)
+++ trunk/extensions/Translate/ffs/Gettext.php  2010-09-12 09:40:47 UTC (rev 
72845)
@@ -361,9 +361,7 @@
  * @ingroup FFS
  */
 class GettextFFS extends SimpleFFS {
-       //
-       // READ
-       //
+
        public function readFromVariable( $data ) {
                $authors = array();
 
@@ -374,9 +372,12 @@
 
                # Then messages and everything else
                $parsedData = $this->parseGettext( $data );
-               $parsedData['MESSAGES'] = $this->group->getMangler()->mangle( 
$parsedData['MESSAGES'] );
                $parsedData['AUTHORS'] = $authors;
 
+               foreach ( $parsedData['MESSAGES'] as $key => $value ) {
+                       if ( $value === '' ) unset( 
$parsedData['MESSAGES'][$key] );
+               }
+
                return $parsedData;
        }
 
@@ -390,6 +391,8 @@
         * @todo Refactor method into smaller parts.
         */
        public static function parseGettextData( $data, $useCtxtAsKey = false ) 
{
+               $potmode = false;
+
                // Normalise newlines, to make processing easier lates
                $data = str_replace( "\r\n", "\n", $data );
 
@@ -400,7 +403,6 @@
 
                /* First one isn't an actual message. We'll handle it specially 
below */
                $headerSection = array_shift( $sections );
-
                /* Since this is the header section, we are only interested in 
the tags
                 * and msgid is empty. Somewhere we should extract the header 
comments
                 * too */
@@ -408,6 +410,10 @@
                if ( $match !== null ) {
                        $headerBlock = self::formatForWiki( $match, 'trim' );
                        $headers = self::parseHeaderTags( $headerBlock );
+
+                       // Check for pot-mode by checking if the header is fuzzy
+                       $flags = self::parseFlags( $headerSection );
+                       if ( in_array( 'fuzzy', $flags, true ) ) $potmode = 
true;
                } else {
                        throw new MWException( "Gettext file header was not 
found:\n\n$data" );
                }
@@ -501,18 +507,15 @@
                        }
 
                        // Parse flags
-                       $matches = array();
-                       if ( preg_match( '/^#,(.*)$/mu', $section, $matches ) ) 
{
-                               $flags = array_map( 'trim', explode( ',', 
$matches[1] ) );
-                               foreach ( $flags as $key => $flag ) {
-                                       if ( $flag === 'fuzzy' ) {
-                                               $item['str'] = TRANSLATE_FUZZY 
. $item['str'];
-                                               unset( $flags[$key] );
-                                       }
+                       $flags = self::parseFlags( $section );
+                       foreach ( $flags as $key => $flag ) {
+                               if ( $flag === 'fuzzy' ) {
+                                       $item['str'] = TRANSLATE_FUZZY . 
$item['str'];
+                                       unset( $flags[$key] );
                                }
-                               $item['flags'] = $flags;
                        }
-
+                       $item['flags'] = $flags;
+               
                        // Rest of the comments
                        $matches = array();
                        if ( preg_match_all( '/^#(.?) (.*)$/m', $section, 
$matches, PREG_SET_ORDER ) ) {
@@ -529,7 +532,7 @@
                                $key = self::generateKeyFromItem( $item );
                        }
 
-                       $messages[$key] = $item['str'];
+                       $messages[$key] = $potmode ? $item['id'] : $item['str'];
                        $template[$key] = $item;
                }
 
@@ -541,6 +544,15 @@
                );
        }
 
+       public static function parseFlags( $section ) {
+               $matches = array();
+               if ( preg_match( '/^#,(.*)$/mu', $section, $matches ) ) {
+                       return array_map( 'trim', explode( ',', $matches[1] ) );
+               } else {
+                       return array();
+               }
+       }
+
        public static function expectKeyword( $name, $section ) {
                /* Catches the multiline textblock that comes after keywords 
msgid,
                 * msgstr, msgid_plural, msgctxt.
@@ -613,12 +625,9 @@
        // WRITE
        //
        protected function writeReal( MessageCollection $collection ) {
-               throw new MWException( 'Not implemented' );
-               $output  = $this->doHeader( $collection );
-               $output .= $this->doAuthors( $collection );
+               $output = $this->doHeader( $collection );
 
                $mangler = $this->group->getMangler();
-
                $messages = array();
                foreach ( $collection as $key => $m ) {
                        $key = $mangler->unmangle( $key );
@@ -639,13 +648,24 @@
 
        protected function doHeader( MessageCollection $collection ) {
                global $wgSitename;
-
                $code = $collection->code;
                $name = TranslateUtils::getLanguageName( $code );
                $native = TranslateUtils::getLanguageName( $code, true );
-               $output  = "# Messages for $name ($native)\n";
-               $output .= "# Exported from $wgSitename\n";
+               $authors = $this->doAuthors( $collection );
+               if ( isset( $this->extra['header'] ) ) {
+                       $extra = "# --\n" . $this->extra['header'];
+               } else {
+                       $extra = '';
+               }
 
+               $output = <<<PHP
+# Translation of {$this->group->getLabel()} to $name ($native)
+# Expored from $wgSitename
+#
+$authors$extra
+
+PHP;
+
                return $output;
        }
 



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to