Example #1
0
 /**
  * Translate text using google api
  *
  * @param   string $text   text to translate
  * @param   string $source source language
  * @param   string $target target language
  *
  * @return string
  *
  * @throws Exception
  */
 public function translate($text, $source, $target)
 {
     // Convert from JISO to ISO codes
     $source = $this->convertFromJisoToIso($source);
     $target = $this->convertFromJisoToIso($target);
     $apiKey = NenoSettings::get('translator_api_key');
     $url = 'https://www.googleapis.com/language/translate/v2';
     //Chunk the text if need be
     $chunks = NenoHelper::chunkHTMLString($text, 4900);
     $translatedChunks = array();
     foreach ($chunks as $chunk) {
         // Invoke the POST request.
         $response = $this->post($url, array('key' => $apiKey, 'q' => $chunk, 'source' => $source, 'target' => $target), array('X-HTTP-Method-Override' => 'GET'));
         // Log it if server response is not OK.
         if ($response->code != 200) {
             NenoLog::log('Google API failed with response: ' . $response->code, 1);
             $responseData = json_decode($response->body, true);
             throw new Exception($responseData['error']['errors'][0]['message'] . ' (' . $responseData['error']['errors'][0]['reason'] . ')', $response->code);
         } else {
             $responseBody = json_decode($response->body);
             $translatedChunks[] = $responseBody->data->translations[0]->translatedText;
         }
     }
     return implode(' ', $translatedChunks);
 }
Example #2
0
 /**
  * Translate text using google api
  *
  * @param   string $text   text to translate
  * @param   string $source source language
  * @param   string $target target language
  *
  * @return string
  *
  * @throws Exception
  */
 public function translate($text, $source, $target)
 {
     // Convert from JISO to ISO codes
     $source = $this->convertFromJisoToIso($source);
     $target = $this->convertFromJisoToIso($target);
     list($clientId, $clientSecret) = explode(':', NenoSettings::get('translator_api_key'));
     $accessToken = $this->requestAccessToken($clientId, $clientSecret);
     $url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate';
     //Chunk the text if need be
     $chunks = NenoHelper::chunkHTMLString($text, 4900);
     $translatedChunks = array();
     foreach ($chunks as $chunk) {
         $query = http_build_query(array('from' => $source, 'text' => $chunk, 'to' => $target));
         // Invoke the POST request.
         $response = $this->get($url . '?' . $query, array('Authorization' => 'Bearer ' . $accessToken));
         $responseBody = (array) simplexml_load_string($response->body);
         // Log it if server response is not OK.
         if ($response->code != 200) {
             NenoLog::log('Bing API failed with response: ' . $response->code, 1);
             throw new Exception((string) $responseBody['body']->p[1], $response->code);
         } else {
             $translatedChunks[] = $responseBody[0];
         }
     }
     return implode(' ', $translatedChunks);
 }
Example #3
0
 /**
  * Translate text using yandex api
  *
  * @param   string $text   text to translate
  * @param   string $source source language
  * @param   string $target target language
  *
  * @return string
  *
  * @throws Exception
  */
 public function translate($text, $source, $target)
 {
     // Convert from JISO to ISO codes
     $target = $this->convertFromJisoToIso($target);
     // Language parameter for url
     $source = $this->convertFromJisoToIso($source);
     $lang = $source . "-" . $target;
     $apiKey = NenoSettings::get('translator_api_key');
     //Chunk the text if need be
     $chunks = NenoHelper::chunkHTMLString($text, 9900);
     $translatedChunks = array();
     foreach ($chunks as $chunk) {
         $url = 'https://translate.yandex.net/api/v1.5/tr.json/translate?key=' . $apiKey . '&lang=' . $lang;
         // Invoke the POST request.
         $response = $this->post($url, array('text' => $chunk));
         // Log it if server response is not OK.
         if ($response->code != 200) {
             NenoLog::log('Yandex API failed with response: ' . $response->code, 1);
             $responseData = json_decode($response->body, true);
             throw new Exception(JText::_('COM_NENO_EDITOR_YANDEX_ERROR_CODE_' . $responseData['code']), $responseData['code']);
         } else {
             $responseBody = json_decode($response->body);
             $translatedChunks[] = $responseBody->text[0];
         }
     }
     return implode(' ', $translatedChunks);
 }