You are here

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

Same name and namespace in other branches
  1. 3.2.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  2. 3.3.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::downloadDocument()
  3. 3.4.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 644

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();
    $target_status = $this
      ->getTargetStatus($component, $langcode);
    $data = [];
    try {
      if ($this->lingotek
        ->getDocumentTranslationStatus($document_id, $locale) !== FALSE || $target_status === Lingotek::STATUS_INTERMEDIATE) {
        $data = $this->lingotek
          ->downloadDocument($document_id, $locale);
      }
      else {
        \Drupal::logger('lingotek')
          ->warning('Avoided download for interface translation component %component: Source status is %source_status, target %target_langcode is %target_status.', [
          '%component' => $component,
          '%source_status' => $this
            ->getSourceStatus($component),
          '%target_langcode' => $langcode,
          '%target_status' => $target_status,
        ]);
        return NULL;
      }
    } catch (LingotekDocumentNotFoundException $exception) {
      $this
        ->setDocumentId($component, NULL);
      $this
        ->deleteMetadata($component);
      throw $exception;
    } catch (LingotekApiException $exception) {
      \Drupal::logger('lingotek')
        ->error('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) {
        $transaction
          ->rollBack();
        \Drupal::logger('lingotek')
          ->error('Error happened (unknown) saving %document_id %locale: %message', [
          '%document_id' => $document_id,
          '%locale' => $locale,
          '%message' => $exception
            ->getMessage(),
        ]);
        $this
          ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
        return FALSE;
      }
      return TRUE;
    }
  }
  \Drupal::logger('lingotek')
    ->warning('Error happened trying to download (%component): no document id found.', [
    '%component' => $component,
  ]);
  return FALSE;
}