You are here

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

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

Downloads a document from the Lingotek service for all available locales.

Parameters

string $component: The component being downloaded.

Return value

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

Overrides LingotekInterfaceTranslationServiceInterface::downloadDocuments

File

src/LingotekInterfaceTranslationService.php, line 774

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function downloadDocuments($component) {
  if ($document_id = $this
    ->getDocumentId($component)) {
    $source_status = $this
      ->getSourceStatus($component);
    $target_languages = $this->languageManager
      ->getLanguages();
    $target_languages = array_filter($target_languages, function (LanguageInterface $language) {
      $configLanguage = ConfigurableLanguage::load($language
        ->getId());
      return $this->lingotekConfiguration
        ->isLanguageEnabled($configLanguage);
    });
    $source_langcode = 'en';
    foreach ($target_languages as $langcode => $language) {
      $locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode);
      if ($langcode !== $source_langcode) {
        try {
          if ($this->lingotek
            ->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
            $data = $this->lingotek
              ->downloadDocument($document_id, $locale);
            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 (LingotekDocumentNotFoundException $exception) {
                $this
                  ->setDocumentId($entity, NULL);
                $this
                  ->deleteMetadata($entity);
                throw $exception;
              } catch (LingotekApiException $exception) {

                // TODO: log issue
                $this
                  ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
                throw $exception;
              } catch (LingotekContentEntityStorageException $storageException) {
                $this
                  ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
                throw $storageException;
              } catch (\Exception $exception) {
                $transaction
                  ->rollBack();
                $this
                  ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
              }
            }
            else {
              return NULL;
            }
          }
        } catch (LingotekDocumentNotFoundException $exception) {
          $this
            ->setDocumentId($entity, NULL);
          $this
            ->deleteMetadata($entity);
          throw $exception;
        } catch (LingotekApiException $exception) {

          // TODO: log issue
          $this
            ->setTargetStatus($component, $langcode, Lingotek::STATUS_ERROR);
          throw $exception;
        }
      }
    }
  }
  return FALSE;
}