You are here

public function LingotekManagementFormBase::downloadTranslation in Lingotek Translation 3.5.x

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  2. 4.0.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  3. 3.0.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  4. 3.1.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  5. 3.2.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  6. 3.3.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  7. 3.4.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  8. 3.6.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  9. 3.7.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()
  10. 3.8.x src/Form/LingotekManagementFormBase.php \Drupal\lingotek\Form\LingotekManagementFormBase::downloadTranslation()

Download translation for a given content in a given language.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity.

string $langcode: The language to download.

File

src/Form/LingotekManagementFormBase.php, line 1340

Class

LingotekManagementFormBase
Form for bulk management of content.

Namespace

Drupal\lingotek\Form

Code

public function downloadTranslation(ContentEntityInterface $entity, $langcode, $job_id, &$context) {

  // We need to reload the entity, just in case we are using the split bulk upload. The metadata isn't true anymore.
  // ToDo: Look for a better way of invalidating already loaded metadata.
  $entity = $this->entityTypeManager
    ->getStorage($entity
    ->getEntityTypeId())
    ->load($entity
    ->id());
  $context['message'] = $this
    ->t('Downloading translation for @type %label in language @language.', [
    '@type' => $entity
      ->getEntityType()
      ->getLabel(),
    '%label' => $entity
      ->label(),
    '@language' => $langcode,
  ]);
  $bundleInfos = $this->entityTypeBundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  if (!$entity
    ->getEntityType()
    ->isTranslatable() || !$bundleInfos[$entity
    ->bundle()]['translatable']) {
    \Drupal::messenger()
      ->addWarning(t('Cannot download @type %label translation for @language. That @bundle_label is not enabled for translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
      '@language' => $langcode,
    ]));
    return;
  }
  if (!$this->lingotekConfiguration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $this
      ->messenger()
      ->addWarning(t('Cannot download @type %label translation for @language. That @bundle_label is not enabled for Lingotek translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
      '@language' => $langcode,
    ]));
    return;
  }
  $locale = $this->languageLocaleMapper
    ->getLocaleForLangcode($langcode);
  if ($profile = $this->lingotekConfiguration
    ->getEntityProfile($entity, FALSE)) {
    try {
      $this->translationService
        ->downloadDocument($entity, $locale);
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The download for @entity_type %title translation failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekContentEntityStorageException $storage_exception) {
      \Drupal::logger('lingotek')
        ->error('The download for @entity_type %title failed because of the length of one field translation (%locale) value: %table.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
        '%locale' => $locale,
        '%table' => $storage_exception
          ->getTable(),
      ]);
      $this
        ->messenger()
        ->addError(t('The download for @entity_type %title failed because of the length of one field translation (%locale) value: %table.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
        '%locale' => $locale,
        '%table' => $storage_exception
          ->getTable(),
      ]));
    }
  }
  else {
    $bundleInfos = $this->entityTypeBundleInfo
      ->getBundleInfo($entity
      ->getEntityTypeId());
    $this
      ->messenger()
      ->addWarning($this
      ->t('The @type %label has no profile assigned so it was not processed.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
    ]));
  }
}