You are here

public function LingotekApi::addTranslation in Lingotek Translation 3.7.x

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

Adds a document target translation for a given locale from the Lingotek service.

Parameters

string $id: The document id.

string $locale: The target locale.

string $workflow_id: (Optional) The workflow ID.

Return value

\Psr\Http\Message\ResponseInterface A response.

Overrides LingotekApiInterface::addTranslation

File

src/Remote/LingotekApi.php, line 319

Class

LingotekApi
A simple connector to the Lingotek Translation API.

Namespace

Drupal\lingotek\Remote

Code

public function addTranslation($id, $locale, $workflow_id = NULL, $vault_id = NULL) {
  try {
    $this->logger
      ->debug('Lingotek::addTranslation called with id ' . $id . ' and locale ' . $locale);
    $args = [
      'locale_code' => $locale,
    ];
    if ($workflow_id) {
      $args['workflow_id'] = $workflow_id;
    }
    if ($vault_id) {
      $args['vault_id'] = $vault_id;
    }
    $response = $this->lingotekClient
      ->post('/api/document/' . $id . '/translation', $args);
  } catch (ClientException $e) {
    if ($e
      ->getCode() === Response::HTTP_NOT_FOUND) {
      $responseBody = json_decode($e
        ->getResponse()
        ->getBody(), TRUE);
      $message = $responseBody['messages'][0];
      throw new LingotekDocumentNotFoundException($message, Response::HTTP_NOT_FOUND);
    }
    elseif ($e
      ->getCode() === Response::HTTP_BAD_REQUEST) {
      $responseBody = json_decode($e
        ->getResponse()
        ->getBody(), TRUE);
      if ($responseBody['messages'][0] === 'Translation (' . $locale . ') already exists.') {
        $this->logger
          ->info('Added an existing target for %id with %args.', [
          '%id' => $id,
          '%args' => var_export($args, TRUE),
        ]);
      }
      return new Response('Was already requested. All is good.', Response::HTTP_CREATED);
    }
    throw new LingotekApiException('Failed to add translation: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  } catch (\Exception $e) {
    $this->logger
      ->error('Error requesting translation (%id, %args): %message.', [
      '%id' => $id,
      '%args' => var_export($args, TRUE),
      '%message' => $e
        ->getMessage(),
    ]);
    throw new LingotekApiException('Failed to add translation: ' . $e
      ->getMessage());
  }
  $this->logger
    ->debug('addTranslation response received, code %code and body %body', [
    '%code' => $response
      ->getStatusCode(),
    '%body' => (string) $response
      ->getBody(TRUE),
  ]);
  return $response;
}