Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 function requestTranslation(JobInterface $job)
 {
     // Add a debug message.
     $job->addMessage('Test translator called.', array(), 'debug');
     // Do something different based on the action, if defined.
     $action = $job->getSetting('action') ?: '';
     switch ($action) {
         case 'submit':
             $job->submitted('Test submit.');
             break;
         case 'reject':
             $job->rejected('This is not supported.');
             break;
         case 'fail':
             // Target not reachable.
             $job->addMessage('Service not reachable.', array(), 'error');
             break;
         case 'translate':
         default:
             // The dummy translation prefixes strings with the target language.
             $data = array_filter(\Drupal::service('tmgmt.data')->flatten($job->getData()), array(\Drupal::service('tmgmt.data'), 'filterData'));
             $tdata = array();
             foreach ($data as $key => $value) {
                 if ($job->getTargetLangcode() != $job->getRemoteTargetLanguage()) {
                     $tdata[$key]['#text'] = $job->getTargetLangcode() . '(' . $job->getRemoteTargetLanguage() . '): ' . $value['#text'];
                 } else {
                     $tdata[$key]['#text'] = $job->getTargetLangcode() . ': ' . $value['#text'];
                 }
             }
             $job->submitted('Test translation created.');
             $job->addTranslatedData(\Drupal::service('tmgmt.data')->unflatten($tdata));
             break;
     }
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function requestTranslation(JobInterface $job)
 {
     // Pull the source data array through the job and flatten it.
     $data = \Drupal::service('tmgmt.data')->filterTranslatable($job->getData());
     $translation = array();
     foreach ($data as $key => $value) {
         // Query the translator API.
         try {
             $result = $this->doRequest($job->getTranslator(), 'Translate', array('from' => $job->getRemoteSourceLanguage(), 'to' => $job->getRemoteTargetLanguage(), 'contentType' => 'text/plain', 'text' => $value['#text']), array('Content-Type' => 'text/plain'));
             // Lets use DOMDocument for now because this service enables us to
             // send an array of translation sources, and we will probably use
             // this soon.
             $dom = new \DOMDocument();
             $dom->loadXML($result->getBody()->getContents());
             $items = $dom->getElementsByTagName('string');
             $translation[$key]['#text'] = $items->item(0)->nodeValue;
             // The translation job has been successfully submitted.
             $job->submitted('The translation job has been submitted.');
             // Save the translated data through the job.
             $job->addTranslatedData(\Drupal::service('tmgmt.data')->unflatten($translation));
         } catch (RequestException $e) {
             $job->rejected('Rejected by Microsoft Translator: !error', array('!error' => $e->getResponse()->getBody()->getContents()), 'error');
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Implements TMGMTTranslatorPluginControllerInterface::requestTranslation().
  */
 public function requestTranslation(JobInterface $job)
 {
     // Pull the source data array through the job and flatten it.
     $data = \Drupal::service('tmgmt.data')->filterTranslatable($job->getData());
     $translation = array();
     $q = array();
     $keys_sequence = array();
     $i = 0;
     // Build Google q param and preserve initial array keys.
     foreach ($data as $key => $value) {
         $q[] = $value['#text'];
         $keys_sequence[] = $key;
     }
     try {
         // Split $q into chunks of self::qChunkSize.
         foreach (array_chunk($q, $this->qChunkSize) as $_q) {
             // Get translation from Google.
             $result = $this->googleRequestTranslation($job, $_q);
             // Collect translated texts with use of initial keys.
             foreach ($result['data']['translations'] as $translated) {
                 $translation[$keys_sequence[$i]]['#text'] = $translated['translatedText'];
                 $i++;
             }
         }
         // The translation job has been successfully submitted.
         $job->submitted('The translation job has been submitted.');
         // Save the translated data through the job.
         // NOTE that this line of code is reached only in case all translation
         // requests succeeded.
         $job->addTranslatedData(\Drupal::service('tmgmt.data')->unflatten($translation));
     } catch (TMGMTException $e) {
         $job->rejected('Translation has been rejected with following error: !error', array('!error' => $e->getMessage()), 'error');
     }
 }