public function LingotekApi::addTranslation in Lingotek Translation 3.8.x
Same name and namespace in other branches
- 8 src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 8.2 src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 4.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.0.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.1.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.2.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.3.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.4.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.5.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.6.x src/Remote/LingotekApi.php \Drupal\lingotek\Remote\LingotekApi::addTranslation()
- 3.7.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 320
Class
- LingotekApi
- A simple connector to the Lingotek Translation API.
Namespace
Drupal\lingotek\RemoteCode
public function addTranslation($id, $locale, $workflow_id = NULL, $vault_id = NULL) {
try {
$this->logger
->debug('Lingotek::addTranslation called with %id and %locale', [
'%id' => $id,
'%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;
}