Index: trunk/extensions/Translate/Translate.php |
— | — | @@ -344,3 +344,8 @@ |
345 | 345 | } |
346 | 346 | |
347 | 347 | $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 @@ |
36 | 36 | </code> |
37 | 37 | |
38 | 38 | == Changes in ??? == |
| 39 | +* 2010-01-24 |
| 40 | +- Support for Apertium machine translation service |
| 41 | +- Fixed issue with jQuery and Vector skin |
39 | 42 | * 2010-01-22 |
40 | 43 | - Support for using Google's translation api as a source for tm suggestions |
41 | 44 | - Interface element for copying tm suggestion into text area |
Index: trunk/extensions/Translate/utils/TranslationHelpers.php |
— | — | @@ -201,6 +201,8 @@ |
202 | 202 | $boxes = (array) $this->getTmBox(); |
203 | 203 | $google = $this->getGoogleSuggestion(); |
204 | 204 | if ( $google ) $boxes[] = $google; |
| 205 | + $apertium = $this->getApertiumSuggestion(); |
| 206 | + if ( $apertium ) $boxes[] = $apertium; |
205 | 207 | |
206 | 208 | // Enclose if there is more than one box |
207 | 209 | if ( count( $boxes ) ) { |
— | — | @@ -258,6 +260,69 @@ |
259 | 261 | } |
260 | 262 | } |
261 | 263 | |
| 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 | + |
262 | 327 | protected function getDefinitionBox() { |
263 | 328 | $en = $this->getDefinition(); |
264 | 329 | if ( $en === null ) return null; |