r70678 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70677‎ | r70678 | r70679 >
Date:08:22, 8 August 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
New task: With suggestions from translation memory
Modified paths:
  • /trunk/extensions/Translate/Translate.i18n.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateTasks.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -117,7 +117,7 @@
118118 /**
119119 * Define various web services that provide translation suggestions.
120120 * Example for tmserver translation memory from translatetoolkit.
121 - * $wgTranslateTranslationServices['local'] = array(
 121+ * $wgTranslateTranslationServices['tmserver'] = array(
122122 * 'server' => 'http://127.0.0.1',
123123 * 'port' => 54321,
124124 * 'timeout-sync' => 3,
@@ -164,6 +164,7 @@
165165 'view' => 'ViewMessagesTask',
166166 'untranslated' => 'ViewUntranslatedTask',
167167 'optional' => 'ViewOptionalTask',
 168+ 'suggestions' => 'ViewWithSuggestionsTask',
168169 // 'untranslatedoptional' => 'ViewUntranslatedOptionalTask',
169170 'review' => 'ReviewMessagesTask',
170171 'reviewall' => 'ReviewAllMessagesTask',
Index: trunk/extensions/Translate/TranslateTasks.php
@@ -170,6 +170,53 @@
171171 }
172172 }
173173
 174+class ViewWithSuggestionsTask extends ViewMessagesTask {
 175+ protected $id = 'suggestions';
 176+
 177+ protected function preinit() {
 178+ $code = $this->options->getLanguage();
 179+ global $wgTranslateTranslationServices;
 180+ $config = $wgTranslateTranslationServices['tmserver'];
 181+ $server = $config['server'];
 182+ $port = $config['port'];
 183+ $timeout = $config['timeout-sync'];
 184+
 185+ $this->collection = $this->group->initCollection( $code );
 186+ $this->collection->setInfile( $this->group->load( $code ) );
 187+ $this->collection->filter( 'ignored' );
 188+ $this->collection->filter( 'optional' );
 189+ $this->collection->filter( 'translated' );
 190+ $this->collection->filter( 'fuzzy' );
 191+ $this->collection->loadTranslations();
 192+
 193+ $start = time();
 194+
 195+ foreach ( $this->collection->keys() as $key => $_ ) {
 196+ if ( time() - $start > 5 || TranslationHelpers::checkTranslationServiceFailure( 'tmserver' ) ) {
 197+ unset( $this->collection[$key] );
 198+ continue;
 199+ }
 200+
 201+ $def = rawurlencode( $this->collection[$key]->definition() );
 202+ $url = "$server:$port/tmserver/en/$code/unit/$def";
 203+ $suggestions = Http::get( $url, $timeout );
 204+
 205+ if ( $suggestions !== false ) {
 206+ $suggestions = FormatJson::decode( $suggestions, true );
 207+ foreach ( $suggestions as $s ) {
 208+ // We have a good suggestion, do not filter
 209+ if ( $s['quality'] > 0.80 ) {
 210+ continue 2;
 211+ }
 212+ }
 213+ } else {
 214+ TranslationHelpers::reportTranslationSerficeFailure( 'tmserver' );
 215+ }
 216+ unset( $this->collection[$key] );
 217+ }
 218+ }
 219+}
 220+
174221 class ViewUntranslatedOptionalTask extends ViewOptionalTask {
175222 protected $id = 'untranslatedoptional';
176223
@@ -375,7 +422,7 @@
376423
377424 class TranslateTasks {
378425 public static function getTasks( $pageTranslation = false ) {
379 - global $wgTranslateTasks;
 426+ global $wgTranslateTasks, $wgTranslateTranslationServices;
380427
381428 // Tasks not to be available in page translation
382429 $filterTasks = array(
@@ -396,6 +443,10 @@
397444 }
398445 }
399446
 447+ if ( !isset( $wgTranslateTranslationServices['tmserver'] ) ) {
 448+ unset( $allTasks['suggestions'] );
 449+ }
 450+
400451 return $allTasks;
401452 }
402453
Index: trunk/extensions/Translate/Translate.i18n.php
@@ -21,6 +21,7 @@
2222 'translate-task-view' => 'View all messages from',
2323 'translate-task-untranslated' => 'View all untranslated messages from',
2424 'translate-task-optional' => 'View optional messages from',
 25+ 'translate-task-suggestions' => 'View messages with suggestions from translation memory',
2526 'translate-task-untranslatedoptional' => 'View untranslated optional messages from',
2627 'translate-task-problematic' => 'View messages that have problems',
2728 'translate-task-review' => 'Review changes to',

Status & tagging log