You are here

public function TestTranslator::requestJobItemsTranslation in Translation Management Tool 8

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 144

Class

TestTranslator
Test source plugin implementation.

Namespace

Drupal\tmgmt_test\Plugin\tmgmt\Translator

Code

public function requestJobItemsTranslation(array $job_items) {
  $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'];
      }
    }
    $job_item
      ->addTranslatedData($data_service
      ->unflatten($tdata));
  }
  $groups = \Drupal::state()
    ->get('job_item_groups') ?: [];
  $groups[] = $group;
  \Drupal::state()
    ->set('job_item_groups', $groups, $group);
}