You are here

public function LingotekContentTranslationService::addTarget in Lingotek Translation 8

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

Request a translation for a given entity in the given locale.

Parameters

ContentEntityInterface &$entity: The entity which target we want to add.

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

Overrides LingotekContentTranslationServiceInterface::addTarget

File

src/LingotekContentTranslationService.php, line 538
Contains \Drupal\lingotek\LingotekContentTranslationService.

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function addTarget(ContentEntityInterface &$entity, $locale) {
  $source_langcode = $entity
    ->getUntranslated()
    ->language()
    ->getId();
  $source_locale = $this->languageLocaleMapper
    ->getLocaleForLangcode($source_langcode);
  if ($locale == $source_locale) {

    // We don't want to translate from one language to itself.
    return FALSE;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    $source_status = $this
      ->getSourceStatus($entity);
    $current_status = $this
      ->getTargetStatus($entity, $drupal_language
      ->id());

    // When a translation is in one of these states, we know that it hasn't yet been sent up to the Lingotek API,
    // which means that we'll have to call addTarget() on it.
    //
    // TODO: should we consider STATUS_NONE as a "pristine" status?
    $pristine_statuses = [
      Lingotek::STATUS_REQUEST,
      Lingotek::STATUS_UNTRACKED,
      Lingotek::STATUS_EDITED,
    ];
    if (in_array($current_status, $pristine_statuses)) {
      if ($this->lingotek
        ->addTarget($document_id, $locale, $this->lingotekConfiguration
        ->getEntityProfile($entity))) {
        $this
          ->setTargetStatus($entity, $drupal_language
          ->id(), Lingotek::STATUS_PENDING);

        // 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($entity, Lingotek::STATUS_CURRENT);
        }
        return TRUE;
      }
    }
  }
  return FALSE;
}