r61438 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61437‎ | r61438 | r61439 >
Date:22:27, 23 January 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Support Apertium machine translation, after all it's free software
Modified paths:
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/utils/TranslationHelpers.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -344,3 +344,8 @@
345345 }
346346
347347 $wgTranslateTM = false;
 348+
 349+/**
 350+ * Set to the url of Apertium Machine Translation service to activate.
 351+ */
 352+$wgTranslateApertium = false;
\ No newline at end of file
Index: trunk/extensions/Translate/README
@@ -35,6 +35,9 @@
3636 </code>
3737
3838 == Changes in ??? ==
 39+* 2010-01-24
 40+- Support for Apertium machine translation service
 41+- Fixed issue with jQuery and Vector skin
3942 * 2010-01-22
4043 - Support for using Google's translation api as a source for tm suggestions
4144 - Interface element for copying tm suggestion into text area
Index: trunk/extensions/Translate/utils/TranslationHelpers.php
@@ -201,6 +201,8 @@
202202 $boxes = (array) $this->getTmBox();
203203 $google = $this->getGoogleSuggestion();
204204 if ( $google ) $boxes[] = $google;
 205+ $apertium = $this->getApertiumSuggestion();
 206+ if ( $apertium ) $boxes[] = $apertium;
205207
206208 // Enclose if there is more than one box
207209 if ( count( $boxes ) ) {
@@ -258,6 +260,69 @@
259261 }
260262 }
261263
 264+ protected function getApertiumSuggestion() {
 265+ global $wgTranslateApertium, $wgMemc;
 266+
 267+ if ( !$wgTranslateApertium ) return null;
 268+
 269+ $page = $this->page;
 270+ $code = $this->targetLanguage;
 271+ $ns = $this->title->getNamespace();
 272+
 273+ $memckey = wfMemckey( 'translate-tmsug-apertium' );
 274+ $pairs = $wgMemc->get( $memckey );
 275+
 276+ if ( !$pairs ) {
 277+ $pairs = array();
 278+ $pairlist = Http::get( $wgTranslateApertium, 5 );
 279+ if ( $pairlist === false ) return null;
 280+ $pairlist = trim( Sanitizer::stripAllTags( $pairlist ) );
 281+ $pairlist = explode( " ", $pairlist );
 282+ foreach ( $pairlist as $pair ) {
 283+ $pair = trim( $pair );
 284+ if ( $pair === '' ) continue;
 285+ $languages = explode( '-', $pair );
 286+ if ( count( $languages ) !== 2 ) continue;
 287+
 288+ list( $source, $target ) = $languages;
 289+ if ( !isset( $pairs[$target] ) ) $pairs[$target] = array();
 290+ $pairs[$target][$source] = true;
 291+ }
 292+
 293+ $wgMemc->set( $memckey, $pairs, 60*60*24 );
 294+ }
 295+
 296+ $code = str_replace( '-', '_', wfBCP47( $code ) );
 297+
 298+ if ( !isset( $pairs[$code] ) ) return;
 299+
 300+ $suggestions = array();
 301+
 302+ foreach ( $pairs[$code] as $candidate => $unused ) {
 303+ $mwcode = str_replace( '_', '-', strtolower($candidate));
 304+ $text = TranslateUtils::getMessageContent( $page, $mwcode, $ns );
 305+ if ( $text === null || TranslateEditAddons::hasFuzzyString( $text ) ) continue;
 306+ $title = Title::makeTitleSafe( $ns, "$page/$mwcode" );
 307+ if ( $title && TranslateEditAddons::isFuzzy( $title ) ) continue;
 308+
 309+ $query = array(
 310+ 'mark' => 0,
 311+ 'mode' => "$candidate-$code",
 312+ 'text' => $text
 313+ );
 314+
 315+ $response = Http::get( "$wgTranslateApertium?" . wfArrayToCgi( $query ), 3 );
 316+ if ( $response === false ) {
 317+ break; // Too slow, back off
 318+ } else {
 319+ $response = $this->suggestionField( Sanitizer::decodeCharReferences( $response ) );
 320+ $suggestions[] = Html::rawElement( 'div', null, self::legend( "Apertium ($candidate)" ) . $response . self::clear() );
 321+ }
 322+ }
 323+ if ( !count($suggestions) ) return null;
 324+ return implode( "\n", $suggestions );
 325+ }
 326+
262327 protected function getDefinitionBox() {
263328 $en = $this->getDefinition();
264329 if ( $en === null ) return null;

Status & tagging log