You are here

public function LingotekManagementFormBase::requestTranslation in Lingotek Translation 3.4.x

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

Request translations 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 1286

Class

LingotekManagementFormBase
Form for bulk management of content.

Namespace

Drupal\lingotek\Form

Code

public function requestTranslation(ContentEntityInterface $entity, $langcode, $job_id, &$context) {
  $context['message'] = $this
    ->t('Requesting translation for @type %label to 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 request @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 request @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
        ->addTarget($entity, $locale);
    } catch (LingotekPaymentRequiredException $exception) {
      $this
        ->messenger()
        ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
    } catch (LingotekDocumentArchivedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has been archived. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekDocumentLockedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has a new version. The document id has been updated for all future interactions. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The request for @entity_type %title translation failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    }
  }
  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(),
    ]));
  }
}