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
 /**
  * Implements TMGMTFileExportInterface::export().
  */
 public function export(JobInterface $job, $conditions = array())
 {
     $items = array();
     foreach ($job->getItems($conditions) as $item) {
         $data = \Drupal::service('tmgmt.data')->filterTranslatable($item->getData());
         foreach ($data as $key => $value) {
             $items[$item->id()][$this->encodeIdSafeBase64($item->id() . '][' . $key)] = $value;
         }
     }
     $elements = array('#theme' => 'tmgmt_file_html_template', '#tjid' => $job->id(), '#source_language' => $job->getRemoteSourceLanguage(), '#target_language' => $job->getRemoteTargetLanguage(), '#items' => $items);
     return \Drupal::service('renderer')->renderPlain($elements);
 }
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');
         }
     }
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function export(JobInterface $job, $conditions = array())
 {
     $this->job = $job;
     $this->openMemory();
     $this->setIndent(TRUE);
     $this->setIndentString(' ');
     $this->startDocument('1.0', 'UTF-8');
     // Root element with schema definition.
     $this->startElement('xliff');
     $this->writeAttribute('version', '1.2');
     $this->writeAttribute('xmlns', 'urn:oasis:names:tc:xliff:document:1.2');
     $this->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
     $this->writeAttribute('xsi:schemaLocation', 'urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd');
     // File element.
     $this->startElement('file');
     $this->writeAttribute('original', 'xliff-core-1.2-strict.xsd');
     $this->writeAttribute('source-language', $job->getRemoteSourceLanguage());
     $this->writeAttribute('target-language', $job->getRemoteTargetLanguage());
     $this->writeAttribute('datatype', 'plaintext');
     // Date needs to be in ISO-8601 UTC
     $this->writeAttribute('date', date('Y-m-d\\Th:m:i\\Z'));
     $this->startElement('header');
     $this->startElement('phase-group');
     $this->startElement('phase');
     $this->writeAttribute('tool-id', 'tmgmt');
     $this->writeAttribute('phase-name', 'extraction');
     $this->writeAttribute('process-name', 'extraction');
     $this->writeAttribute('job-id', $job->id());
     $this->endElement();
     $this->endElement();
     $this->startElement('tool');
     $this->writeAttribute('tool-id', 'tmgmt');
     $this->writeAttribute('tool-name', 'Drupal Translation Management Tools');
     $this->endElement();
     $this->endElement();
     $this->startElement('body');
     foreach ($job->getItems($conditions) as $item) {
         $this->addItem($item);
     }
     // End the body, file and xliff tags.
     $this->endElement();
     $this->endElement();
     $this->endElement();
     $this->endDocument();
     return $this->outputMemory();
 }