You are here

public function SmartlingTranslator::requestTranslation in TMGMT Translator Smartling 8

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.2 src/Plugin/tmgmt/Translator/SmartlingTranslator.php \Drupal\tmgmt_smartling\Plugin\tmgmt\Translator\SmartlingTranslator::requestTranslation()
  3. 8.3 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()

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

File

src/Plugin/tmgmt/Translator/SmartlingTranslator.php, line 129
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) {
  $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');
  }
  try {
    $upload_params = [
      'approved' => 0,
      'smartling.placeholder_format_custom' => $job
        ->getSetting('custom_regexp_placeholder'),
    ];
    if ($job
      ->getSetting('auto_authorize_locales')) {
      $upload_params['localesToApprove[0]'] = $job
        ->getRemoteTargetLanguage();
    }
    if ($job
      ->getTranslator()
      ->getSetting('callback_url_use')) {
      $upload_params['callbackUrl'] = Url::fromRoute('tmgmt_smartling.push_callback', [
        'job' => $job
          ->id(),
      ])
        ->setOptions(array(
        'absolute' => TRUE,
      ))
        ->toString();
    }
    $this
      ->getSmartlingApi($job
      ->getTranslator())
      ->uploadFile(\Drupal::service('file_system')
      ->realpath($file
      ->getFileUri()), $file
      ->getFilename(), $export_format === 'xlf' ? 'xliff' : $export_format, $upload_params);
    $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', array(
      '@error' => $e
        ->getMessage(),
      '@file' => $file
        ->getFileUri(),
    ), 'error');
  }

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