http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73312
Revision: 73312
Author: nikerabbit
Date: 2010-09-19 11:37:52 +0000 (Sun, 19 Sep 2010)
Log Message:
-----------
Removed custom Gettext formatter from export-to-file task.
Refactored to use GettextFFS. In the process fixed few bugs:
some missing fuzzy tags and comments from wrong namespace.
Modified Paths:
--------------
trunk/extensions/Translate/Groups.php
trunk/extensions/Translate/TranslateTasks.php
trunk/extensions/Translate/ffs/Gettext.php
Modified: trunk/extensions/Translate/Groups.php
===================================================================
--- trunk/extensions/Translate/Groups.php 2010-09-19 11:08:09 UTC (rev
73311)
+++ trunk/extensions/Translate/Groups.php 2010-09-19 11:37:52 UTC (rev
73312)
@@ -350,6 +350,29 @@
* custom type of message groups.
*/
class FileBasedMessageGroup extends MessageGroupBase {
+
+ /**
+ * Constructs a FileBasedMessageGroup from any normal message group.
+ * Useful for doing special Gettext exports from any group.
+ * @param $group MessageGroup
+ * @return FileBasedMessageGroup
+ */
+ public static function newFromMessageGroup( $group ) {
+ $conf = array(
+ 'BASIC' => array(
+ 'class' => 'FileBasedMessageGroup',
+ 'id' => $group->getId(),
+ 'label' => $group->getLabel(),
+ 'namespace' => $group->getNamespace(),
+ ),
+ 'FILES' => array(
+ 'sourcePattern' => '',
+ 'targetPattern' => '',
+ ),
+ );
+ return MessageGroupBase::factory( $conf );
+ }
+
public function exists() {
return $this->getFFS()->exists();
}
Modified: trunk/extensions/Translate/TranslateTasks.php
===================================================================
--- trunk/extensions/Translate/TranslateTasks.php 2010-09-19 11:08:09 UTC
(rev 73311)
+++ trunk/extensions/Translate/TranslateTasks.php 2010-09-19 11:37:52 UTC
(rev 73312)
@@ -406,126 +406,25 @@
return true;
}
- /// @todo Encapsulate Gettext formatter.
public function output() {
- global $wgServer, $wgTranslateDocumentationLanguageCode;
+ $ffs = null;
+ if ( $this->group instanceof FileBasedMessageGroup ) {
+ $ffs = $this->group->getFFS();
+ }
+
+ if ( !$ffs instanceof GettextFFS ) {
+ $group = FileBasedMessageGroup::newFromMessageGroup(
$this->group );
+ $ffs = new GettextFFS( $group );
+ }
- $lang = Language::factory( 'en' );
+ $ffs->setOfflineMode( 'true' );
- $out = '';
- $now = wfTimestampNow();
- $label = $this->group->getLabel();
$code = $this->options->getLanguage();
- $languageName = TranslateUtils::getLanguageName( $code );
-
- $filename = $code . '_' . $this->group->getID() . '.po';
+ $id = $this->group->getID();
+ $filename = "${id}_$code.po";
header( "Content-Disposition: attachment;
filename=\"$filename\"" );
-
- $headers = array();
- $headers['Project-Id-Version'] = 'MediaWiki ' .
SpecialVersion::getVersion( 'nodb' );
- /// @todo Make this customisable or something.
- $headers['Report-Msgid-Bugs-To'] = $wgServer;
- /// @todo Language::sprintfDate() does not support any time
zone flags.
- $headers['POT-Creation-Date'] = $lang->sprintfDate(
'xnY-xnm-xnd xnH:xni:xns+0000', $now );
- $headers['Language-Team'] = TranslateUtils::getLanguageName(
$this->options->getLanguage() );
- $headers['Content-Type'] = 'text-plain; charset=UTF-8';
- $headers['Content-Transfer-Encoding'] = '8bit';
- $headers['X-Generator'] = 'MediaWiki Translate extension ' .
TRANSLATE_VERSION;
- $headers['X-Language-Code'] = $this->options->getLanguage();
- $headers['X-Message-Group'] = $this->group->getId();
-
- $headerlines = array( '' );
- foreach ( $headers as $key => $value ) {
- $headerlines[] = "$key: $value\n";
- }
-
- $out .= "# Translation of $label to $languageName\n";
- $out .= self::formatmsg( '', $headerlines );
-
- foreach ( $this->collection as $key => $m ) {
- $flags = array();
-
- $translation = $m->translation();
- // CASE2: no translation.
- if ( $translation === null ) {
- $translation = '';
- }
-
- // CASE3: optional messages; accept only if different.
- if ( $m->hasTag( 'optional' ) ) {
- $flags[] = 'optional';
- }
-
- // Remove fuzzy markings before export, use the fuzzy
flag.
- if ( strpos( $translation, TRANSLATE_FUZZY ) !== false
) {
- $translation = str_replace( TRANSLATE_FUZZY,
'', $translation );
- $flags[] = 'fuzzy';
- }
-
- $comments = '';
- if ( $wgTranslateDocumentationLanguageCode ) {
- $documentation =
TranslateUtils::getMessageContent( $key, $wgTranslateDocumentationLanguageCode
);
- if ( $documentation ) {
- $comments = $documentation;
- }
- }
-
- $out .= self::formatComments( $comments, $flags );
- $out .= self::formatmsg( $m->definition(),
$translation, $key, $flags );
-
- }
-
- return $out;
+ return $ffs->writeIntoVariable( $this->collection );
}
-
- private static function escape( $line ) {
- $line = addcslashes( $line, '\\"' );
- $line = str_replace( "\n", '\n', $line );
- $line = '"' . $line . '"';
-
- return $line;
- }
-
- private static function formatComments( $comments = false, $flags =
false ) {
- $output = array();
-
- if ( $comments ) {
- $output[] = '#. ' . implode( "\n#. ", explode( "\n",
$comments ) );
- }
-
- if ( $flags ) {
- $output[] = '#, ' . implode( ', ', $flags );
- }
-
- if ( !count( $output ) ) {
- $output[] = '#:';
- }
-
- return implode( "\n", $output ) . "\n";
- }
-
- private static function formatmsg( $msgid, $msgstr, $msgctxt = false ) {
- $output = array();
-
- if ( $msgctxt ) {
- $output[] = 'msgctxt ' . self::escape( $msgctxt );
- }
-
- if ( !is_array( $msgid ) ) {
- $msgid = array( $msgid );
- }
-
- if ( !is_array( $msgstr ) ) {
- $msgstr = array( $msgstr );
- }
-
- $output[] = 'msgid ' . implode( "\n", array_map( array(
__CLASS__, 'escape' ), $msgid ) );
- $output[] = 'msgstr ' . implode( "\n", array_map( array(
__CLASS__, 'escape' ), $msgstr ) );
-
- $out = implode( "\n", $output ) . "\n\n";
-
- return $out;
- }
}
/**
Modified: trunk/extensions/Translate/ffs/Gettext.php
===================================================================
--- trunk/extensions/Translate/ffs/Gettext.php 2010-09-19 11:08:09 UTC (rev
73311)
+++ trunk/extensions/Translate/ffs/Gettext.php 2010-09-19 11:37:52 UTC (rev
73312)
@@ -51,7 +51,12 @@
}
/**
- * Ugly hack to avoid code duplication between old and new style FFS.
+ * Parses gettext data into internal representation.
+ * @param $data \string
+ * @param $useCtxtAsKey \bool Whether to create message keys from the
context
+ * or use msgctxt (non-standard po-files)
+ * @param $mangler StringMangler
+ * @return \array
* @todo Refactor method into smaller parts.
*/
public static function parseGettextData( $data, $useCtxtAsKey = false,
$mangler ) {
@@ -335,15 +340,23 @@
$specs['Project-Id-Version'] = $this->group->getLabel();
$specs['Report-Msgid-Bugs-To'] = $wgSitename;
$specs['PO-Revision-Date'] = self::formatTime( wfTimestampNow()
);
- $specs['X-POT-Import-Date'] = self::formatTime(
$this->getPotTime() );
+ if ( $this->offlineMode ) {
+ $specs['POT-Creation-Date'] = self::formatTime(
wfTimestampNow() );
+ } else {
+ $specs['X-POT-Import-Date'] = self::formatTime(
$this->getPotTime() );
+ }
$specs['Language-Team'] = "$name <$portal>";
$specs['Content-Type'] = 'text/plain; charset=UTF-8';
$specs['Content-Transfer-Encoding'] = '8bit';
$specs['X-Generator'] = $this->getGenerator();
$specs['X-Translation-Project'] = "$wgSitename at $wgServer";
$specs['X-Language-Code'] = $code;
- // Prepend # so that message import does not think this is a
file it can import
- $specs['X-Message-Group'] = '#' . $this->group->getId();
+ if ( $this->offlineMode ) {
+ $specs['X-Message-Group'] = $this->group->getId();
+ } else {
+ // Prepend # so that message import does not think this
is a file it can import
+ $specs['X-Message-Group'] = '#' . $this->group->getId();
+ }
$plural = self::getPluralRule( $code );
if ( $plural ) {
$specs['Plural-Forms'] = $plural;
@@ -394,9 +407,13 @@
$flags = self::chainGetter( 'flags', $pot, $trans, array() );
$flags = array_merge( $m->getTags(), $flags );
- $ctxt = self::chainGetter( 'ctxt', $pot, $trans, false );
- if ( $ctxt ) {
- $content .= 'msgctxt ' . self::escape( $ctxt ) . "\n";
+ if ( $this->offlineMode ) {
+ $content .= 'msgctxt ' . self::escape( $key ) . "\n";
+ } else {
+ $ctxt = self::chainGetter( 'ctxt', $pot, $trans, false
);
+ if ( $ctxt ) {
+ $content .= 'msgctxt ' . self::escape( $ctxt )
. "\n";
+ }
}
$msgid = $m->definition();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs