You are here

public function LingotekApi::addTranslation in Lingotek Translation 8

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

Overrides LingotekApiInterface::addTranslation

File

src/Remote/LingotekApi.php, line 225
Contains \Drupal\lingotek\Remote\LingotekApi.

Class

LingotekApi

Namespace

Drupal\lingotek\Remote

Code

public function addTranslation($id, $locale, $workflow_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;
    }
    $response = $this->lingotekClient
      ->post('/api/document/' . $id . '/translation', $args);
  } catch (\Exception $e) {

    // If the problem is that the translation already exist, don't fail.
    if ($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 \GuzzleHttp\Psr7\Response(Response::HTTP_CREATED);
    }
    $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;
}