You are here

public function LingotekInterfaceTranslationService::downloadDocument in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 4.0.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  2. 3.2.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  3. 3.3.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  4. 3.5.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  5. 3.6.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  6. 3.7.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  7. 3.8.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()

Downloads a document from the Lingotek service for a given locale.

Parameters

string $component: The component being downloaded.

string $locale: Lingotek translation language which we want to download.

Return value

bool TRUE if the document was downloaded successfully, FALSE if not.

Overrides LingotekInterfaceTranslationServiceInterface::downloadDocument

File

src/LingotekInterfaceTranslationService.php, line 600

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function downloadDocument($component, $locale) {
  if ($document_id = $this
    ->getDocumentId($component)) {
    $source_status = $this
      ->getSourceStatus($component);
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    $langcode = $drupal_language
      ->id();
    $data = [];
    try {
      if ($this->lingotek
        ->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
        $data = $this->lingotek
          ->downloadDocument($document_id, $locale);
      }
      else {
        \Drupal::logger('lingotek')
          ->warning($this
          ->t('Avoided download for (%component): Source status is %source_status.', [
          '%component' => $component,
          '%source_status' => $this
            ->getSourceStatus($component),
        ]));
        return NULL;
      }
    } catch (LingotekApiException $exception) {
      \Drupal::logger('lingotek')
        ->error($this
        ->t('Error happened downloading %document_id %locale: %message', [
        '%document_id' => $document_id,
        '%locale' => $locale,
        '%message' => $exception
          ->getMessage(),
      ]));
      $this
        ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
      throw $exception;
    }
    if ($data) {

      // Check the real status, because it may still need review or anything.
      $status = $this->lingotek
        ->getDocumentTranslationStatus($document_id, $locale);
      $transaction = $this->connection
        ->startTransaction();
      try {
        $saved = $this
          ->saveTargetData($component, $langcode, $data);
        if ($saved) {

          // If the status was "Importing", and the target was added
          // successfully, we can ensure that the content is current now.
          if ($source_status == Lingotek::STATUS_IMPORTING) {
            $this
              ->setSourceStatus($component, Lingotek::STATUS_CURRENT);
          }
          if ($source_status == Lingotek::STATUS_EDITED) {
            $this
              ->setTargetStatus($component, $langcode, Lingotek::STATUS_EDITED);
          }
          elseif ($status === TRUE) {
            $this
              ->setTargetStatus($component, $langcode, Lingotek::STATUS_CURRENT);
          }
          else {
            $this
              ->setTargetStatus($component, $langcode, Lingotek::STATUS_INTERMEDIATE);
          }
        }
      } catch (\Exception $exception) {
        \Drupal::logger('lingotek')
          ->error($this
          ->t('Error happened (unknown) saving %document_id %locale: %message', [
          '%document_id' => $document_id,
          '%locale' => $locale,
          '%message' => $exception
            ->getMessage(),
        ]));
        $this
          ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
        $transaction
          ->rollBack();
        return FALSE;
      }
      return TRUE;
    }
  }
  \Drupal::logger('lingotek')
    ->warning($this
    ->t('Error happened trying to download (%component): no document id found.', [
    '%component' => $component,
  ]));
  return FALSE;
}