r91796 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91795‎ | r91796 | r91797 >
Date:18:29, 9 July 2011
Author:nikerabbit
Status:deferred
Tags:
Comment:
Implement per-group default source language. Based on patch by Harald Stowasser.
Fixes bug 29664.
Modified paths:
  • /trunk/extensions/Translate/FFS.php (modified) (history)
  • /trunk/extensions/Translate/Groups.php (modified) (history)
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateEditAddons.php (modified) (history)
  • /trunk/extensions/Translate/TranslateTasks.php (modified) (history)
  • /trunk/extensions/Translate/scripts/tm-export.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/tm-export.php
@@ -4,7 +4,7 @@
55 *
66 * @author Niklas Laxstrom
77 *
8 - * @copyright Copyright © 2010, Niklas Laxström
 8+ * @copyright Copyright © 2010-2011, Niklas Laxström
99 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1010 * @file
1111 */
@@ -48,7 +48,8 @@
4949 $capitalized = MWNamespace::isCapitalized( $group->getNamespace() );
5050 $ns_text = $wgContLang->getNsText( $group->getNamespace() );
5151
52 - foreach ( $group->load( 'en' ) as $key => $definition ) {
 52+ $definitions = $group->load( $group->getSourceLanguage() );
 53+ foreach ( $definitions as $key => $definition ) {
5354 // TODO: would be nice to do key normalisation closer to the message groups, to avoid transforming back and forth.
5455 // But how to preserve the original keys...
5556 $key = strtr( $key, ' ', '_' );
@@ -75,7 +76,7 @@
7677 'text' => $definition,
7778 'context' => "$ns_text:$key",
7879 'length' => strlen( $definition ),
79 - 'lang' => 'en'
 80+ 'lang' => $group->getSourceLanguage(),
8081 );
8182
8283 $source_id = $dbw->selectField( '`sources`', 'sid', $insert, __METHOD__ );
Index: trunk/extensions/Translate/FFS.php
@@ -8,7 +8,7 @@
99 * @file
1010 * @defgroup FFS File format support
1111 * @author Niklas Laxström
12 - * @copyright Copyright © 2008-2010, Niklas Laxström
 12+ * @copyright Copyright © 2008-2011, Niklas Laxström
1313 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1414 */
1515
@@ -89,7 +89,7 @@
9090 public function setWritePath( $writePath ) { $this->writePath = $writePath; }
9191 public function getWritePath() { return $this->writePath; }
9292
93 - public function exists( $code = 'en' ) {
 93+ public function exists( $code ) {
9494 $filename = $this->group->getSourceFilePath( $code );
9595 if ( $filename === null ) {
9696 return false;
@@ -1077,10 +1077,11 @@
10781078
10791079 public function write( MessageCollection $collection ) {
10801080 if ( $this->fw === null ) {
1081 - $outputFile = $this->writePath . '/' . $this->group->getTargetFilename( 'en' );
 1081+ $sourceLanguage = $this->group->getSourceLanguage();
 1082+ $outputFile = $this->writePath . '/' . $this->group->getTargetFilename( $sourceLanguage );
10821083 wfMkdirParents( dirname( $outputFile ), null, __METHOD__ );
10831084 $this->fw = fopen( $outputFile, 'w' );
1084 - $this->fw = fopen( $this->writePath . '/' . $this->group->getTargetFilename( 'en' ), 'w' );
 1085+ $this->fw = fopen( $this->writePath . '/' . $this->group->getTargetFilename( $sourceLanguage ), 'w' );
10851086 fwrite( $this->fw, "# -*- coding: utf-8 -*-\nmsg = {\n" );
10861087 }
10871088
Index: trunk/extensions/Translate/MessageGroups.php
@@ -5,7 +5,7 @@
66 *
77 * @file
88 * @author Niklas Laxström
9 - * @copyright Copyright © 2008-2010, Niklas Laxström
 9+ * @copyright Copyright © 2008-2011, Niklas Laxström
1010 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1111 */
1212
@@ -69,6 +69,10 @@
7070 public function isMeta() { return $this->meta; }
7171 public function setMeta( $value ) { $this->meta = $value; }
7272
 73+ public function getSourceLanguage() {
 74+ return 'en';
 75+ }
 76+
7377 /**
7478 * To avoid key conflicts between groups or separated changed messages between
7579 * branches one can set a message key mangler.
@@ -113,7 +117,7 @@
114118 * @return Array of messages definitions indexed by key.
115119 */
116120 public function getDefinitions() {
117 - $defs = $this->load( 'en' );
 121+ $defs = $this->load( $this->getSourceLanguage() );
118122 if ( !is_array( $defs ) ) {
119123 throw new MWException( "Unable to load definitions for " . $this->getLabel() );
120124 }
@@ -244,6 +248,10 @@
245249
246250 return isset( $tags[$type] ) ? $tags[$type] : array();
247251 }
 252+
 253+ protected function isSourceLanguage( $code ) {
 254+ return $code === $this->getSourceLanguage();
 255+ }
248256 }
249257
250258 /**
@@ -334,7 +342,7 @@
335343 );
336344
337345 if ( $this->parentId ) {
338 - if ( $code !== 'en' ) {
 346+ if ( !$this->isSourceLanguage( $code ) ) {
339347 // For branches, load newer compatible messages for missing entries, if any.
340348 $trunk = MessageGroups::getGroup( $this->parentId );
341349 $messages += $trunk->mangler->unmangle( $trunk->load( $code ) );
@@ -430,7 +438,7 @@
431439 }
432440
433441 if ( $desc === null ) {
434 - $desc = $this->getMessage( $key, 'en' );
 442+ $desc = $this->getMessage( $key, $this->getSourceLanguage() );
435443 }
436444
437445 if ( $desc !== null ) {
@@ -503,7 +511,7 @@
504512 }
505513
506514 public function exists() {
507 - return is_readable( $this->getMessageFileWithPath( 'en' ) );
 515+ return is_readable( $this->getMessageFileWithPath( $this->getSourceLanguage() ) );
508516 }
509517
510518 public function getChecker() {
@@ -543,7 +551,7 @@
544552 public function initCollection( $code, $unique = false ) {
545553 $collection = parent::initCollection( $code, $unique );
546554
547 - $defs = $this->load( 'en' );
 555+ $defs = $this->load( $this->getSourceLanguage() );
548556 foreach ( $defs as $key => $value ) {
549557 $collection[$key] = new FatMessage( $key, implode( ", ", $value ) );
550558 }
@@ -628,7 +636,7 @@
629637 $data = file_get_contents( dirname( __FILE__ ) . '/wikimedia-mostused-2011.txt' );
630638 $data = str_replace( "\r", '', $data );
631639 $messages = explode( "\n", $data );
632 - $contents = Language::getMessagesFor( 'en' );
 640+ $contents = Language::getMessagesFor( $this->getSourceLanguage() );
633641 $definitions = array();
634642
635643 foreach ( $messages as $key ) {
@@ -641,7 +649,6 @@
642650 }
643651 }
644652
645 -
646653 /**
647654 * Group for messages that can be controlled via a page in %MediaWiki namespace.
648655 *
@@ -666,6 +673,12 @@
667674 $this->source = $source;
668675 }
669676
 677+ /// Defaults to wiki content language.
 678+ public function getSourceLanguage() {
 679+ global $wgLanguagecode;
 680+ return $wgLanguagecode;
 681+ }
 682+
670683 /**
671684 * Fetch definitions from database.
672685 * @return \array Array of messages keys with definitions.
@@ -721,6 +734,12 @@
722735 $this->namespaces = array( NS_TRANSLATIONS, NS_TRANSLATIONS_TALK );
723736 }
724737
 738+ /// Defaults to wiki content language.
 739+ public function getSourceLanguage() {
 740+ global $wgLanguagecode;
 741+ return $wgLanguagecode;
 742+ }
 743+
725744 public function getTitle() {
726745 if ( is_string( $this->title ) ) {
727746 $this->title = Title::newFromText( $this->title );
@@ -757,7 +776,7 @@
758777 }
759778
760779 public function load( $code ) {
761 - if ( $code === 'en' ) {
 780+ if ( $this->isSourceLanguage( $code ) ) {
762781 return $this->getDefinitions();
763782 }
764783
@@ -773,8 +792,8 @@
774793 * @return \mixed Stored translation or null.
775794 */
776795 public function getMessage( $key, $code ) {
777 - if ( $code === 'en' ) {
778 - $stuff = $this->load( 'en' );
 796+ if ( $this->isSourceLanguage( $code ) ) {
 797+ $stuff = $this->load( $code );
779798 return isset( $stuff[$key] ) ? $stuff[$key] : null;
780799 }
781800
Index: trunk/extensions/Translate/TranslateEditAddons.php
@@ -6,7 +6,7 @@
77 * @file
88 * @author Niklas Laxström
99 * @author Siebrand Mazeland
10 - * @copyright Copyright © 2007-2010 Niklas Laxström, Siebrand Mazeland
 10+ * @copyright Copyright © 2007-2011 Niklas Laxström, Siebrand Mazeland
1111 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1212 */
1313
@@ -36,7 +36,7 @@
3737 return true;
3838 }
3939
40 - $collection = $group->initCollection( 'en' );
 40+ $collection = $group->initCollection( $group->getSourceLanguage() );
4141 $collection->filter( 'optional' );
4242 $keys = array_keys( $collection->keys() );
4343 $count = count( $keys );
@@ -370,7 +370,7 @@
371371 $checker = $group->getChecker();
372372
373373 if ( $checker ) {
374 - $en = $group->getMessage( $key, 'en' );
 374+ $en = $group->getMessage( $key, $group->getSourceLanguage() );
375375 $message = new FatMessage( $key, $en );
376376 /**
377377 * Take the contents from edit field as a translation.
@@ -418,7 +418,9 @@
419419 return true;
420420 }
421421
422 - $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), "$key/en" );
 422+
 423+
 424+ $definitionTitle = Title::makeTitleSafe( $title->getNamespace(), "$key/" . $group->getSourceLanguage() );
423425 if ( $definitionTitle && $definitionTitle->exists() ) {
424426 $definitionRevision = $definitionTitle->getLatestRevID();
425427
Index: trunk/extensions/Translate/Translate.php
@@ -15,7 +15,7 @@
1616 /**
1717 * Version number used in extension credits and in other placed where needed.
1818 */
19 -define( 'TRANSLATE_VERSION', '2011-06-24' );
 19+define( 'TRANSLATE_VERSION', '2011-07-09' );
2020
2121 /**
2222 * Extension credits properties.
Index: trunk/extensions/Translate/TranslateTasks.php
@@ -263,6 +263,7 @@
264264 $server = $config['server'];
265265 $port = $config['port'];
266266 $timeout = $config['timeout-sync'];
 267+ $sourceLanguage = $this->group->getSourceLanguage();
267268
268269 $this->collection = $this->group->initCollection( $code );
269270 $this->collection->setInfile( $this->group->load( $code ) );
@@ -282,7 +283,7 @@
283284 }
284285
285286 $def = rawurlencode( $this->collection[$key]->definition() );
286 - $url = "$server:$port/tmserver/en/$code/unit/$def";
 287+ $url = "$server:$port/tmserver/$sourceLanguage/$code/unit/$def";
287288 $suggestions = Http::get( $url, $timeout );
288289
289290 if ( $suggestions !== false ) {
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -132,7 +132,7 @@
133133 return;
134134 }
135135
136 - $this->definition = $this->group->getMessage( $this->page, 'en' );
 136+ $this->definition = $this->group->getMessage( $this->page, $this->group->getSourceLanguage() );
137137
138138 return $this->definition;
139139 }
@@ -641,10 +641,17 @@
642642 wfMsg( 'word-separator' ) .
643643 wfMsg( 'parentheses', $title );
644644
 645+ // Source language object
 646+ $sl = Language::factory( $this->group->getSourceLanguage() );
 647+
645648 $dialogID = $this->dialogID();
646649 $id = Sanitizer::escapeId( "def-$dialogID" );
647650 $msg = $this->adder( $id ) . "\n" . Html::rawElement( 'div',
648 - array( 'class' => 'mw-translate-edit-deftext', 'dir' => 'ltr', 'lang' => 'en' ),
 651+ array(
 652+ 'class' => 'mw-translate-edit-deftext',
 653+ 'dir' => $sl->getDir(),
 654+ 'lang' => $sl->getCode(),
 655+ ),
649656 TranslateUtils::convertWhiteSpaceToHTML( $en )
650657 );
651658
@@ -825,7 +832,7 @@
826833 if ( $ffs instanceof GettextFFS ) {
827834 global $wgContLang;
828835 $mykey = $wgContLang->lcfirst( $this->page );
829 - $data = $ffs->read( 'en' );
 836+ $data = $ffs->read( $this->group->getSourceLanguage() );
830837 $help = $data['TEMPLATE'][$mykey]['comments'];
831838 // Do not display an empty comment. That's no help and takes up unnecessary space.
832839 $conf = $this->group->getConfiguration();
Index: trunk/extensions/Translate/Groups.php
@@ -4,7 +4,7 @@
55 *
66 * @file
77 * @author Niklas Laxström
8 - * @copyright Copyright © 2010, Niklas Laxström
 8+ * @copyright Copyright © 2010-2011, Niklas Laxström
99 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1010 */
1111
@@ -106,6 +106,12 @@
107107 * @return string or null
108108 */
109109 public function getMessage( $key, $code );
 110+
 111+ /**
 112+ * Returns language code depicting the language of source text.
 113+ * @return string
 114+ */
 115+ public function getSourceLanguage();
110116 }
111117
112118 /**
@@ -137,6 +143,11 @@
138144
139145 public function isMeta() { return $this->getFromConf( 'BASIC', 'meta' ); }
140146
 147+ public function getSourceLanguage() {
 148+ $conf = $this->getFromConf( 'BASIC', 'sourcelanguage' );
 149+ return $conf !== 'null' ? $conf : 'en';
 150+ }
 151+
141152 protected function getFromConf( $section, $key ) {
142153 return isset( $this->conf[$section][$key] ) ? $this->conf[$section][$key] : null;
143154 }
@@ -211,7 +222,7 @@
212223 $cache = new MessageGroupCache( $this );
213224 if ( !$cache->exists() ) {
214225 wfWarn( "By-passing message group cache" );
215 - $messages = $this->load( 'en' );
 226+ $messages = $this->load( $this->getSourceLanguage() );
216227 } else {
217228 foreach ( $cache->getKeys() as $key ) {
218229 $messages[$key] = $cache->get( $key );
@@ -266,7 +277,7 @@
267278
268279 if ( !$cache->exists() ) {
269280 wfWarn( "By-passing message group cache" );
270 - $messageKeys = array_keys( $this->load( 'en' ) );
 281+ $messageKeys = array_keys( $this->load( $this->getSourceLanguage() ) );
271282 } else {
272283 $messageKeys = $cache->getKeys();
273284 }
@@ -346,6 +357,10 @@
347358
348359 return $index;
349360 }
 361+
 362+ protected function isSourceLanguage( $code ) {
 363+ return $code === $this->getSourceLanguage();
 364+ }
350365 }
351366
352367 /**
@@ -390,7 +405,7 @@
391406 }
392407
393408 public function getSourceFilePath( $code ) {
394 - if ( $code === 'en' ) {
 409+ if ( $this->isSourceLanguage( $code ) ) {
395410 $pattern = $this->getFromConf( 'FILES', 'definitionFile' );
396411 if ( $pattern !== null ) {
397412 return $this->replaceVariables( $pattern, $code );
@@ -610,7 +625,7 @@
611626 }
612627 } else {
613628 // BC for MessageGroupOld
614 - $messages += $group->load( 'en' );
 629+ $messages += $group->load( $this->getSourceLanguage() );
615630 }
616631 }
617632

Follow-up revisions

RevisionCommit summaryAuthorDate
r91798Follow-up r91796. Default value is needed.nikerabbit18:45, 9 July 2011
r91821Fu r91796 - brainfartnikerabbit08:17, 10 July 2011

Status & tagging log