public function TestTranslator::requestJobItemsTranslation

Requests the translation of a JobItem.

Parameters

JobItemInterface[] $job_items: The JobItem we want to translate.

Overrides ContinuousTranslatorInterface::requestJobItemsTranslation

1 call to TestTranslator::requestJobItemsTranslation()
TestTranslator::requestTranslation in tmgmt_test/src/Plugin/tmgmt/Translator/TestTranslator.php
@abstract

File

tmgmt_test/src/Plugin/tmgmt/Translator/TestTranslator.php, line 145

Class

TestTranslator
Test source plugin implementation.

Namespace

Drupal\tmgmt_test\Plugin\tmgmt\Translator

Code

public function requestJobItemsTranslation(array $job_items) {

  /** @var \Drupal\tmgmt\Data $data_service */
  $data_service = \Drupal::service('tmgmt.data');
  $group = [];

  /** @var JobItemInterface $job_item */
  foreach ($job_items as $job_item) {
    $job = $job_item
      ->getJob();
    $target_langcode = $job
      ->getTargetLangcode();
    $remote_target_langcode = $job
      ->getRemoteTargetLanguage();
    $group[] = [
      'item_id' => $job_item
        ->id(),
      'job_id' => $job_item
        ->getJobId(),
    ];

    // Add a debug message.
    $job_item
      ->active('Requested translation to the continuous translator.', [], 'debug');

    // The dummy translation prefixes strings with the target language.
    $data = $data_service
      ->filterTranslatable($job_item
      ->getData());
    $tdata = [];
    foreach ($data as $key => $value) {

      // Special handling for path fields that start with the language
      // prefix, keep them valid by just replacing the path prefix.
      if (strpos($value['#text'], '/' . $job
        ->getSourceLangcode()) === 0) {
        $tdata[$key]['#text'] = str_replace('/' . $job
          ->getSourceLangcode(), '/' . $job
          ->getTargetLangcode(), $value['#text']);
      }
      elseif ($target_langcode != $remote_target_langcode) {
        $tdata[$key]['#text'] = $target_langcode . '(' . $remote_target_langcode . '): ' . $value['#text'];
      }
      else {
        $tdata[$key]['#text'] = $target_langcode . ': ' . $value['#text'];
      }
    }

    // Translate files.
    foreach ($data_service
      ->getTranslatableFiles($job_item
      ->getData()) as $key => $source_file) {
      $source_uri = $source_file
        ->getFileUri();
      $source_content = file_get_contents($source_uri);
      if ($target_langcode != $remote_target_langcode) {
        $translation_content = $target_langcode . '(' . $remote_target_langcode . '): ' . $source_content;
      }
      else {
        $translation_content = $target_langcode . ': ' . $source_content;
      }
      $translation_file = $data_service
        ->createFileTranslation($source_file, $target_langcode, $translation_content);
      $tdata[$key]['#file'] = $translation_file
        ->id();
    }
    $job_item
      ->addTranslatedData($data_service
      ->unflatten($tdata));
  }
  $groups = \Drupal::state()
    ->get('job_item_groups') ?: [];
  $groups[] = $group;
  \Drupal::state()
    ->set('job_item_groups', $groups);
}