r72860 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72859‎ | r72860 | r72861 >
Date:14:05, 12 September 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Handle plural rules (not actual plurals yet) and comments on exporting
Modified paths:
  • /trunk/extensions/Translate/ffs/Gettext.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/ffs/Gettext.php
@@ -631,6 +631,23 @@
632632 $potTemplate = isset( $pot['TEMPLATE'][$key] ) ?
633633 $pot['TEMPLATE'][$key] : array();
634634
 635+ $comments = array();
 636+ if ( isset( $potTemplate['comments'] ) ) {
 637+ $comments = $potTemplate['comments'];
 638+ } elseif ( isset( $transTemplate['comments'] ) ) {
 639+ $comments = $transTemplate['comments'];
 640+ }
 641+
 642+ $header = '';
 643+
 644+ $header .= self::formatDocumentation( $key );
 645+ foreach ( $comments as $type => $typecomments ) {
 646+ foreach ( $typecomments as $comment ) {
 647+ if ( strpos( $comment, '[Wiki]' ) === 0 ) continue;
 648+ $header .= "#$type $comment\n";
 649+ }
 650+ }
 651+
635652 $tags = $m->getTags();
636653 $flags = isset( $transTemplate['flags'] ) ? $transTemplate['flags'] : array();
637654
@@ -638,9 +655,16 @@
639656
640657 if ( $outFlags ) {
641658 sort( $outFlags );
642 - $output .= "#, " . implode( ', ', $outFlags ) . "\n";
 659+ $header .= "#, " . implode( ', ', $outFlags ) . "\n";
643660 }
644661
 662+ if ( $header ) {
 663+ $output .= $header;
 664+ } else {
 665+ // Must be at least empty comment
 666+ $output .= "#\n";
 667+ }
 668+
645669 if ( isset( $potTemplate['msgctxt'] ) ) {
646670 $output .= 'msgctxt ' . self::escape( $potTemplate['msgctxt'] ) . "\n";
647671 }
@@ -681,8 +705,8 @@
682706
683707 $specs['Project-Id-Version'] = $this->group->getLabel();
684708 $specs['Report-Msgid-Bugs-To'] = $wgSitename;
685 - $specs['POT-Creation-Date'] = self::formatTime( $this->getPotTime() );
686709 $specs['PO-Revision-Date'] = self::formatTime( wfTimestampNow() );
 710+ $specs['X-POT-Import-Date'] = self::formatTime( $this->getPotTime() );
687711 $specs['Language-Team'] = "$name <$portal>";
688712 $specs['Content-Type'] = 'text/plain; charset=UTF-8';
689713 $specs['Content-Transfer-Encoding'] = '8bit';
@@ -691,6 +715,12 @@
692716 $specs['X-Language-Code'] = $code;
693717 // Prepend # so that message import does not think this is a file it can import
694718 $specs['X-Message-Group'] = '#' . $this->group->getId();
 719+ $plural = self::getPluralRule( $code );
 720+ if ( $plural ) {
 721+ $specs['Plural-Forms'] = $plural;
 722+ } elseif( !isset( $specs['Plural-Forms'] ) ) {
 723+ $specs['Plural-Forms'] = 'nplurals=2; plural=(n != 1);';
 724+ }
695725
696726 $output .= 'msgid ""' . "\n";
697727 $output .= 'msgstr ""' . "\n";
@@ -732,6 +762,23 @@
733763 "; Translate extension (" . TRANSLATE_VERSION . ")";
734764 }
735765
 766+ protected static function formatDocumentation( $key ) {
 767+ global $wgTranslateDocumentationLanguageCode;
 768+
 769+ $code = $wgTranslateDocumentationLanguageCode;
 770+ if ( !$code ) return '';
 771+
 772+ $documentation = TranslateUtils::getMessageContent( $key, $code );
 773+ if ( !is_string( $documentation ) ) return '';
 774+
 775+ $lines = explode( "\n", $documentation );
 776+ $out = '';
 777+ foreach ( $lines as $line ) {
 778+ $out .= "#. [Wiki] $line\n";
 779+ }
 780+ return $out;
 781+ }
 782+
736783 protected static function escape( $line ) {
737784 // There may be \ as a last character, for keeping trailing whitespace
738785 $line = preg_replace( '/\\\\$/', '', $line );
@@ -742,4 +789,20 @@
743790 return $line;
744791 }
745792
 793+ /**
 794+ * Returns plural rule for Gettext.
 795+ * @param $code \string Language code.
 796+ * @return \string
 797+ */
 798+ public static function getPluralRule( $code ) {
 799+ $rulefile = dirname( __FILE__ ) . '/../data/plural-gettext.txt';
 800+ $rules = file_get_contents( $rulefile );
 801+ foreach ( explode( "\n", $rules ) as $line ) {
 802+ if ( trim( $line ) === '' ) continue;
 803+ list( $rulecode, $rule ) = explode( "\t", $line );
 804+ if ( $rulecode === $code ) return $rule;
 805+ }
 806+ return '';
 807+ }
 808+
746809 }

Status & tagging log