MicrosoftWebService.php

Go to the documentation of this file.
00001 <?php
00017 class MicrosoftWebService extends TranslationWebService {
00018     protected function mapCode( $code ) {
00019         $map = array(
00020             'zh-hant' => 'zh-CHT',
00021             'zh-hans' => 'zh-CHS',
00022         );
00023 
00024         return isset( $map[$code] ) ? $map[$code] : $code;
00025     }
00026 
00027     protected function doPairs() {
00028         if ( !isset( $this->config['key'] ) ) {
00029             throw new TranslationWebServiceException( 'API key is not set' );
00030         }
00031 
00032         $options = array();
00033         $options['method'] = 'GET';
00034         $options['timeout'] = $this->config['timeout'];
00035 
00036         $params = array(
00037             'appId' => $this->config['key'],
00038         );
00039 
00040         $url = 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?';
00041         $url .= wfArrayToCgi( $params );
00042 
00043         $req = MWHttpRequest::factory( $url, $options );
00044         wfProfileIn( 'TranslateWebServiceRequest-' . $this->service . '-pairs' );
00045         $status = $req->execute();
00046         wfProfileOut( 'TranslateWebServiceRequest-' . $this->service . '-pairs' );
00047 
00048         if ( !$status->isOK() ) {
00049             $error = $req->getContent();
00050             // Most likely a timeout or other general error
00051             $exception = 'Http::get failed:' . serialize( $error ) . serialize( $status );
00052             throw new TranslationWebServiceException( $exception );
00053         }
00054 
00055         $xml = simplexml_load_string( $req->getContent() );
00056 
00057         $languages = array();
00058         foreach ( $xml->string as $language ) {
00059             $languages[] = strval( $language );
00060         }
00061 
00062         // Let's make a cartesian product, assuming we can translate from any
00063         // language to any language
00064         $pairs = array();
00065         foreach ( $languages as $from ) {
00066             foreach ( $languages as $to ) {
00067                 $pairs[$from][$to] = true;
00068             }
00069         }
00070 
00071         return $pairs;
00072     }
00073 
00074     protected function doRequest( $text, $from, $to ) {
00075         if ( !isset( $this->config['key'] ) ) {
00076             throw new TranslationWebServiceException( 'API key is not set' );
00077         }
00078 
00079         $text = trim( $text );
00080         $text = $this->wrapUntranslatable( $text );
00081 
00082         $options = array();
00083         $options['timeout'] = $this->config['timeout'];
00084 
00085         $params = array(
00086             'text' => $text,
00087             'from' => $from,
00088             'to' => $to,
00089             'appId' => $this->config['key'],
00090         );
00091 
00092         $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate?';
00093         $url .= wfArrayToCgi( $params );
00094 
00095         $req = MWHttpRequest::factory( $url, $options );
00096         wfProfileIn( 'TranslateWebServiceRequest-' . $this->service );
00097         $status = $req->execute();
00098         wfProfileOut( 'TranslateWebServiceRequest-' . $this->service );
00099 
00100         if ( !$status->isOK() ) {
00101             $error = $req->getContent();
00102             // Most likely a timeout or other general error
00103             $exception = 'Http::get failed: ' . $url . serialize( $error ) . serialize( $status );
00104             throw new TranslationWebServiceException( $exception );
00105         }
00106 
00107         $ret = $req->getContent();
00108         $text = preg_replace( '~<string.*>(.*)</string>~', '\\1', $ret );
00109         $text = Sanitizer::decodeCharReferences( $text );
00110 
00111         return $this->unwrapUntranslatable( $text );
00112     }
00113 }
Generated on Tue Oct 29 00:00:26 2013 for MediaWiki Translate Extension by  doxygen 1.6.3