You are here

function tmgmt_smartling_download_file in TMGMT Translator Smartling 8

Same name and namespace in other branches
  1. 8.4 tmgmt_smartling.module \tmgmt_smartling_download_file()
  2. 8.2 tmgmt_smartling.module \tmgmt_smartling_download_file()
  3. 8.3 tmgmt_smartling.module \tmgmt_smartling_download_file()
3 calls to tmgmt_smartling_download_file()
PushCallbackController::callback in src/Controller/PushCallbackController.php
SmartlingTranslator::downloadTranslation in src/Plugin/tmgmt/Translator/SmartlingTranslator.php
Downloads translation file and applies it.
tmgmt_smartling_download_file_submit in ./tmgmt_smartling.module

File

./tmgmt_smartling.module, line 20
Contains

Code

function tmgmt_smartling_download_file(JobInterface $job) {
  try {
    $smartlingApi = $job
      ->getTranslatorPlugin()
      ->getSmartlingApi($job
      ->getTranslator());
    $retrieval_type = $job
      ->getTranslator()
      ->getSetting('retrieval_type');
    $filename = $job
      ->getTranslatorPlugin()
      ->getFileName($job);
    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    $xml = $smartlingApi
      ->downloadFile($filename, $job
      ->getRemoteTargetLanguage(), [
      'retrievalType' => $retrieval_type,
    ]);
  } catch (\Exception $e) {
    Drupal::logger('smartling')
      ->error($e
      ->getMessage());
    return FALSE;
  }
  $path = $job
    ->getSetting('scheme') . '://tmgmt_smartling_translations/' . $job
    ->getTranslatorPlugin()
    ->getFileName($job);
  $dirname = dirname($path);
  if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && ($file = file_save_data($xml, $path, FILE_EXISTS_REPLACE))) {
    $plugin = \Drupal::service('plugin.manager.tmgmt_file.format')
      ->createInstance($extension);
    if ($plugin) {

      // Validate the file on job.
      if (!$plugin
        ->validateImport($file
        ->getFileUri(), $job)) {
        $job
          ->addMessage('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ], 'error');
        \Drupal::logger('tmgmt_smartling')
          ->error('Failed to validate file @file. Import for job @job_id aborted.', [
          '@file' => $file
            ->getFileUri(),
          '@job_id' => $job
            ->id(),
        ]);
        return FALSE;
      }
      else {
        try {

          // Set active state for all job items of a job in order to be able
          // force translation. It allows to override existing translations
          // that might be in an "Accepted" state.
          // @see JobItem::addTranslatedData() method.
          foreach ($job
            ->getItems() as $item) {
            $item
              ->setState(JobItemInterface::STATE_ACTIVE);
          }

          // Validation successful, start import.
          $job
            ->addTranslatedData($plugin
            ->import($file
            ->getFileUri(), $job));
          $job
            ->addMessage('Successfully imported file.');
        } catch (Exception $e) {
          $job
            ->addMessage('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ], 'error');
          \Drupal::logger('tmgmt_smartling')
            ->error('File import failed with the following message: @message', [
            '@message' => $e
              ->getMessage(),
          ]);
          return FALSE;
        }
      }
    }
  }
  \Drupal::logger('tmgmt_smartling')
    ->info('Translation for "@filename" was successfully downloaded.', [
    '@filename' => $filename,
  ]);
  return TRUE;
}