You are here

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

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

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

Parameters

string $component: The component which target we want to add.

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

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekDocumentNotFoundException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterfaceTranslationServiceInterface::addTarget

File

src/LingotekInterfaceTranslationService.php, line 482

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function addTarget($component, $locale) {
  $source_langcode = 'en';
  $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($component)) {
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    $source_status = $this
      ->getSourceStatus($component);
    $current_status = $this
      ->getTargetStatus($component, $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,
      Lingotek::STATUS_DELETED,
    ];
    if (in_array($current_status, $pristine_statuses)) {
      try {
        $result = $this->lingotek
          ->addTarget($document_id, $locale, NULL);
      } catch (LingotekDocumentNotFoundException $exception) {
        $this
          ->setDocumentId($component, NULL);
        $this
          ->deleteMetadata($component);
        throw $exception;
      } catch (LingotekDocumentLockedException $exception) {
        $this
          ->setDocumentId($component, $exception
          ->getNewDocumentId());
        throw $exception;
      } catch (LingotekDocumentArchivedException $exception) {
        $this
          ->setDocumentId($component, NULL);
        $this
          ->deleteMetadata($component);
        throw $exception;
      } catch (LingotekPaymentRequiredException $exception) {
        throw $exception;
      } catch (LingotekApiException $exception) {
        throw $exception;
      }
      if ($result) {
        $this
          ->setTargetStatus($component, $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($component, Lingotek::STATUS_CURRENT);
        }
        return TRUE;
      }
    }
  }
  return FALSE;
}