You are here

public function Lingotek::addTarget in Lingotek Translation 3.4.x

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

Requests a translation to the Lingotek service.

Parameters

string $doc_id: The document id to translate.

string $locale: The Lingotek locale to request.

\Drupal\lingotek\LingotekProfileInterface $profile: The profile being used.

Return value

bool TRUE if the document was successfully updated. FALSE if not.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterface::addTarget

File

src/Lingotek.php, line 552

Class

Lingotek
The connecting class between Drupal and Lingotek

Namespace

Drupal\lingotek

Code

public function addTarget($doc_id, $locale, LingotekProfileInterface $profile = NULL) {
  $workflow_id = NULL;
  $vault_id = NULL;
  $drupal_language = $this->languageLocaleMapper
    ->getConfigurableLanguageForLocale($locale);
  if ($profile !== NULL && ($workflow_id = $profile
    ->getWorkflowForTarget($drupal_language
    ->getId()))) {
    switch ($workflow_id) {
      case 'project_default':
        $workflow_id = NULL;
        break;
      case 'default':
        $workflow_id = $this->configFactory
          ->get(static::SETTINGS)
          ->get('default.workflow');
        break;
    }
  }
  if ($profile !== NULL && ($vault_id = $profile
    ->getVaultForTarget($drupal_language
    ->getId()))) {
    if ($vault_id === 'default') {
      $vault_id = NULL;
    }
  }
  $response = $this->api
    ->addTranslation($doc_id, $locale, $workflow_id, $vault_id);
  $statusCode = $response
    ->getStatusCode();
  if ($statusCode == Response::HTTP_CREATED) {
    return TRUE;
  }
  elseif ($statusCode == Response::HTTP_PAYMENT_REQUIRED) {

    // This is only applicable to subscription-based connectors, but the
    // recommended action is to present the user with a message letting them
    // know their Lingotek account has been disabled, and to please contact
    // support to re-enable their account.
    $responseBody = Json::decode($response
      ->getBody());
    $message = '';
    if (!empty($responseBody) && isset($responseBody['messages'])) {
      $message = $responseBody['messages'][0];
    }
    throw new LingotekPaymentRequiredException($message);
  }
  elseif ($statusCode == Response::HTTP_GONE) {

    // Set the status of the document back to its pre-uploaded state.
    // Typically this means the state would be set to Upload, or None but this
    // may vary depending on connector. Essentially, the content’s status
    // indicator should show that the source content needs to be re-uploaded
    // to Lingotek.
    return FALSE;
  }
  elseif ($statusCode == Response::HTTP_LOCKED) {

    // Update the connector’s document mapping with the ID provided in the
    // next_document_id within the API response. This new ID represents the
    // new version of the document.
    $responseBody = Json::decode($response
      ->getBody());
    if (empty($responseBody)) {
      return FALSE;
    }
    else {
      $nextDocId = $responseBody['next_document_id'];
      return FALSE;
    }
  }
  return FALSE;
}