You are here

public function SmartlingTranslator::requestTranslation in TMGMT Translator Smartling 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::requestTranslation()
  2. 8 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::requestTranslation()
  3. 8.2 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::requestTranslation()

@abstract

Submits the translation request and sends it to the translation provider.

During the translation process, Job::getItems() will only return job items that are not already fully translated.

Parameters

\Drupal\tmgmt\JobInterface $job: The job that should be submitted.

Overrides TranslatorPluginInterface::requestTranslation

See also

hook_tmgmt_job_before_request_translation()

hook_tmgmt_job_after_request_translation()

2 calls to SmartlingTranslator::requestTranslation()
SmartlingTranslator::requestJobItemsTranslation in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
Requests the translation of a JobItem.
SmartlingTranslator::requestTranslationExtended in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
Requests translation.

File

src/Plugin/tmgmt/Translator/SmartlingTranslator.php, line 230
Contains \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator.

Class

SmartlingTranslator
Smartling translator plugin.

Namespace

Drupal\tmgmt_smartling\Plugin\tmgmt\Translator

Code

public function requestTranslation(JobInterface $job) {
  $batch_uid = $job
    ->getSetting('batch_uid');
  $api_wrapper = $this
    ->getApiWrapper($job
    ->getTranslator()
    ->getSettings());
  $error_notification_message = t('File @name (job id = @job_id) wasn\'t uploaded. Please see logs for more info.', [
    '@name' => $job
      ->getTranslatorPlugin()
      ->getFileName($job),
    '@job_id' => $job
      ->id(),
  ])
    ->render();
  $api_wrapper
    ->createAuditLogRecord($job, NULL, $this->currentUser, CreateRecordParameters::ACTION_TYPE_UPLOAD);

  // Skip processing if job/batch hasn't been created.
  if (empty($batch_uid)) {
    $this->logger
      ->error(t('File @name (job id = @job_id) wasn\'t uploaded due to previous error(s).', [
      '@name' => $job
        ->getTranslatorPlugin()
        ->getFileName($job),
      '@job_id' => $job
        ->id(),
    ])
      ->render());
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => $error_notification_message,
      "type" => "error",
    ]);
    return;
  }
  $name = $this
    ->getFileName($job);
  $export_format = pathinfo($name, PATHINFO_EXTENSION);
  $export = $this->formatPluginsManager
    ->createInstance($export_format);
  $path = $job
    ->getSetting('scheme') . '://tmgmt_sources/' . $name;
  $dirname = dirname($path);
  if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
    $data = $export
      ->export($job);
    $file = file_save_data($data, $path, FILE_EXISTS_REPLACE);
    $this->fileUsage
      ->add($file, 'tmgmt_smartling', 'tmgmt_job', $job
      ->id());
    $job
      ->submitted('Exported file can be downloaded <a href="@link">here</a>.', array(
      '@link' => file_create_url($path),
    ));
  }
  else {
    $e = new \Exception('It is not possible to create a directory ' . $dirname);
    watchdog_exception('tmgmt_smartling', $e);
    $job
      ->rejected('Job has been rejected with following error: @error', [
      '@error' => $e
        ->getMessage(),
    ], 'error');
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => $error_notification_message,
      "type" => "error",
    ]);
  }
  try {
    $upload_params = new UploadFileParameters();
    $upload_params
      ->setClientLibId(BaseApiAbstract::getCurrentClientId(), BaseApiAbstract::getCurrentClientVersion());
    $upload_params
      ->setAuthorized(0);
    if ($job
      ->getTranslator()
      ->getSetting('callback_url_use')) {
      $upload_params
        ->set('callbackUrl', $this
        ->getCallbackUrl($job));
    }
    $real_path = \Drupal::service('file_system')
      ->realpath($file
      ->getFileUri());
    $file_type = $export_format === 'xlf' ? 'xliff' : $export_format;
    $upload_params
      ->setLocalesToApprove($job
      ->getRemoteTargetLanguage());
    $api_wrapper
      ->getApi('batch')
      ->uploadBatchFile($real_path, $file
      ->getFilename(), $file_type, $batch_uid, $this
      ->addSmartlingDirectives($upload_params, $job));
    $message = t('File uploaded. Job id: @job_id, file name: @name.', [
      '@name' => $job
        ->getTranslatorPlugin()
        ->getFileName($job),
      '@job_id' => $job
        ->id(),
    ]);
    $this->logger
      ->info($message);
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => $message
        ->render(),
      "type" => "status",
    ]);
    if ($job
      ->id() == $job
      ->getSetting('batch_execute_on_job')) {
      $api_wrapper
        ->executeBatch($batch_uid);
      $api_wrapper
        ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
        "message" => t("Finished: content is in the job. You may need to wait a few seconds before content is authorized (if you checked 'authorize' checkbox).")
          ->render(),
        "type" => "status",
      ]);
    }
    $this->eventDispatcher
      ->dispatch(RequestTranslationEvent::REQUEST_TRANSLATION_EVENT, new RequestTranslationEvent($job));
  } catch (Exception $e) {
    watchdog_exception('tmgmt_smartling', $e);
    $job
      ->rejected('Job has been rejected with following error: @error uploading @file', [
      '@error' => $e
        ->getMessage(),
      '@file' => $file
        ->getFileUri(),
    ], 'error');
    $api_wrapper
      ->createFirebaseRecord("tmgmt_smartling", "notifications", 10, [
      "message" => t('Error while uploading @file. Please see logs for more info.', [
        '@file' => $file
          ->getFileUri(),
      ])
        ->render(),
      "type" => "error",
    ]);
  }

  // @todo disallow to submit translation to unsupported language.
}