Index: trunk/extensions/Translate/Translate.php |
— | — | @@ -180,6 +180,13 @@ |
181 | 181 | 'timeout-async' => 6, |
182 | 182 | 'type' => 'google', |
183 | 183 | ); |
| 184 | +$wgTranslateTranslationServices['Microsoft'] = array( |
| 185 | + 'url' => 'http://api.microsofttranslator.com/V2/Http.svc/Translate', |
| 186 | + 'key' => null, |
| 187 | + 'timeout-sync' => 3, |
| 188 | + 'timeout-async' => 6, |
| 189 | + 'type' => 'microsoft', |
| 190 | +); |
184 | 191 | $wgTranslateTranslationServices['Apertium'] = array( |
185 | 192 | 'url' => 'http://api.apertium.org/json/translate', |
186 | 193 | 'pairs' => 'http://api.apertium.org/json/listPairs', |
Index: trunk/extensions/Translate/utils/TranslationHelpers.php |
— | — | @@ -314,6 +314,8 @@ |
315 | 315 | $boxes[] = $this->getTmBox( $name, $config ); |
316 | 316 | } elseif ( $config['type'] === 'google' ) { |
317 | 317 | $boxes[] = $this->getGoogleSuggestion( $name, $config ); |
| 318 | + } elseif ( $config['type'] === 'microsoft' ) { |
| 319 | + $boxes[] = $this->getMicrosoftSuggestion( $name, $config ); |
318 | 320 | } elseif ( $config['type'] === 'apertium' ) { |
319 | 321 | $boxes[] = $this->getApertiumSuggestion( $name, $config ); |
320 | 322 | } else { |
— | — | @@ -413,6 +415,67 @@ |
414 | 416 | return $options; |
415 | 417 | } |
416 | 418 | |
| 419 | + protected function getMicrosoftSuggestion( $serviceName, $config ) { |
| 420 | + global $wgMemc; |
| 421 | + |
| 422 | + if ( self::checkTranslationServiceFailure( $serviceName ) ) { |
| 423 | + return null; |
| 424 | + } |
| 425 | + |
| 426 | + $code = $this->targetLanguage; |
| 427 | + $definition = trim( strval( $this->getDefinition() ) ); |
| 428 | + $definition = str_replace( "\n", "<newline/>", $definition ); |
| 429 | + |
| 430 | + $memckey = wfMemckey( 'translate-tmsug-badcodes-' . $serviceName ); |
| 431 | + $unsupported = $wgMemc->get( $memckey ); |
| 432 | + |
| 433 | + if ( $definition === '' || isset( $unsupported[$code] ) ) { |
| 434 | + return null; |
| 435 | + } |
| 436 | + |
| 437 | + global $wgSitename, $wgVersion, $wgProxyKey; |
| 438 | + $options = array(); |
| 439 | + $options['timeout'] = $config['timeout']; |
| 440 | + |
| 441 | + $params = array( |
| 442 | + 'text' => $definition, |
| 443 | + 'to' => $code, |
| 444 | + ); |
| 445 | + |
| 446 | + if ( isset( $config['key'] ) ) { |
| 447 | + $params['appId'] = $config['key']; |
| 448 | + } else { |
| 449 | + return null; |
| 450 | + } |
| 451 | + |
| 452 | + $url = $config['url'] . '?' . wfArrayToCgi( $params ); |
| 453 | + $url = wfExpandUrl( $url ); |
| 454 | + |
| 455 | + $options['method'] = 'GET'; |
| 456 | + $req = HttpRequest::factory( $url, $options ); |
| 457 | + $status = $req->execute(); |
| 458 | + |
| 459 | + if ( !$status->isOK() ) { |
| 460 | + $error = $req->getContent(); |
| 461 | + if ( strpos( $error, 'must be a valid language' ) !== false ) { |
| 462 | + $unsupported[$code] = true; |
| 463 | + $wgMemc->set( $memckey, $unsupported, 60 * 60 * 8 ); |
| 464 | + return null; |
| 465 | + } |
| 466 | + |
| 467 | + wfWarn( __METHOD__ . ': Http::get failed:' . $error ); |
| 468 | + // Most likely a timeout or other general error |
| 469 | + self::reportTranslationServiceFailure( $serviceName ); |
| 470 | + return null; |
| 471 | + } |
| 472 | + |
| 473 | + $ret = $req->getContent(); |
| 474 | + $text = preg_replace( '~<string.*>(.*)</string>~', '\\1', $ret ); |
| 475 | + $text = Sanitizer::decodeCharReferences( $text ); |
| 476 | + $text = $this->suggestionField( $text ); |
| 477 | + return Html::rawElement( 'div', null, self::legend( $serviceName ) . $text . self::clear() ); |
| 478 | + } |
| 479 | + |
417 | 480 | protected function getApertiumSuggestion( $serviceName, $config ) { |
418 | 481 | global $wgMemc; |
419 | 482 | |