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)
 {
     $tuid = $job->getSetting('translator');
     // Create local task for this job.
     $local_task = tmgmt_local_task_create(array('uid' => $job->getOwnerId(), 'tuid' => $tuid, 'tjid' => $job->id(), 'title' => $job->label()));
     // If we have translator then switch to pending state.
     if ($tuid) {
         $local_task->status = LocalTaskInterface::STATUS_PENDING;
     }
     $local_task->save();
     // Create task items.
     foreach ($job->getItems() as $item) {
         $local_task->addTaskItem($item);
     }
     // The translation job has been successfully submitted.
     $job->submitted();
 }
Exemplo n.º 3
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');
         }
     }
 }