You are here

public function GlobalLinkTranslator::retrieveTranslation in GlobalLink Connect for Drupal 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php \Drupal\globallink\Plugin\tmgmt\Translator\GlobalLinkTranslator::retrieveTranslation()

Receives and stores a translation.

Parameters

string $ticket_id: The submission ticket id.

\Drupal\tmgmt\JobInterface $job: The job to retrieve translation for.

1 call to GlobalLinkTranslator::retrieveTranslation()
GlobalLinkTranslator::fetchJobs in src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php
Fetches translations for job items of a given job.

File

src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php, line 509

Class

GlobalLinkTranslator
GlobalLink translation plugin controller.

Namespace

Drupal\globallink\Plugin\tmgmt\Translator

Code

public function retrieveTranslation($ticket_id, JobInterface $job) {
  $translator = $job
    ->getTranslator();
  $settings = $translator
    ->getSettings();
  $pd_config = $this->glExchangeAdapter
    ->getPDConfig($settings);
  $glexchange = $this->glExchangeAdapter
    ->getGlExchange($pd_config);
  try {
    $data = $glexchange
      ->downloadTarget($ticket_id);
    $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA) or die("Error: Cannot create object");
    $body = $xml->file->body;
    $num_items = count($body->group);
    for ($i = 0; $i < $num_items; $i++) {
      $item_ids[$i] = (string) $body->group[$i]
        ->attributes()->{'id'};
    }

    // Remove the preview URL again.
    foreach ($body->group as $group) {
      unset($group->preview);
    }
    $data = $xml
      ->asXML();
    $parsed_data = $this->formatManager
      ->createInstance('xlf')
      ->import($data, FALSE);
    $job
      ->addTranslatedData($parsed_data);
    $glexchange
      ->sendDownloadConfirmation($ticket_id);
  } catch (\Exception $e) {
    $job
      ->addMessage('Failed downloading translation. Message error: @error', [
      '@error' => $e
        ->getMessage(),
    ], 'error');
  }
}