How to translate a text using Translate
To translate text, use the translate method.
Getting started
The Translate API requires you to send your authentication credentials in each request. The authentication method depends on the type of account used to send your request:
-
Get an IAM token to authenticate your Yandex account or federated account. Transmit the token in the
Authorization
header of each request in the following format:Authorization: Bearer <IAM token>
-
Get the ID of any folder for which your account has the
ai.translate.user
role or higher. Make sure to include your folder ID in thefolderId
field in the body of each request.
-
Choose one of the authentication methods:
-
Get an IAM token. Include the IAM token in the
Authorization
header in the following format:Authorization: Bearer <IAM token>
-
Create an API key. Include the API key in the
Authorization
header in the following format:Authorization: Api-Key <API key>
-
-
Assign the service account the
ai.translate.user
role or higher for the folder where it was created.Do not specify the folder ID in your requests, as YandexGPT uses the folder in which the service account was created.
Translating a text from any language
To translate a text from any supported language, provide it using the translate method:
To use the examples, install cURL
The example below is intended to be run in MacOS and Linux. To run it in Windows, see how to work with Bash in Microsoft Windows.
This example shows how to translate two strings containing Hello and World into Russian. The text source language is recognized automatically.
-
Create a file with the request body, e.g.,
body.json
:{ "folderId": "<Folder_ID>", "texts": ["Hello", "World"], "targetLanguageCode": "ru" }
Where:
folderId
: Folder ID received before starting.texts
: Text to translate as a list of strings.targetLanguageCode
: Target language. You can get the language code with a list of supported languages.
-
Submit the file for translation by running the command:
export IAM_TOKEN=<IAM_token> curl \ --request POST \ --header "Content-Type: application/json" \ --header "Authorization: Bearer ${IAM_TOKEN}" \ --data '@<path_to_JSON_file>' \ "https://translate.api.cloud.yandex.net/translate/v2/translate"
Where
IAM_TOKEN
is the IAM token received before starting.The service will return the translated strings:
{ "translations": [ { "text": "Hello", "detectedLanguageCode": "en" }, { "text": "World", "detectedLanguageCode": "en" } ] }
This example shows how to translate the following two lines of text into Russian: Hello
and World
. The source language of a text is recognized automatically.
Create a file with the request body (for example, body.ру
).
import requests
IAM_TOKEN = '<IAM_token>'
folder_id = '<folder_ID>'
target_language = 'ru'
texts = ["Hello", "World"]
body = {
"targetLanguageCode": target_language,
"texts": texts,
"folderId": folder_id,
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {0}".format(IAM_TOKEN)
}
response = requests.post('https://translate.api.cloud.yandex.net/translate/v2/translate',
json = body,
headers = headers
)
print(response.text)
Where:
IAM_TOKEN
: IAM token received before starting.folder_id
: Folder ID received before starting.target_language
: Target language. You can get the language code together with a list of supported languages.texts
: Text to translate as a list of strings.
You can save your IAM token to a file. To do this, specify the file path in the token_path
variable:
body.py
import os
import requests
token_path = '<path_to_file_with_IAM_token>'
folder_id = '<folder_ID>'
target_language = 'ru'
texts = ["Hello", "World"]
IAM_TOKEN = os.path.join(os.getcwd(), token_path)
with open(IAM_TOKEN) as f:
IAM_TOKEN = f.read().strip()
body = {
"targetLanguageCode": target_language,
"texts": texts,
"folderId": folder_id,
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {0}".format(IAM_TOKEN)
}
response = requests.post('https://translate.api.cloud.yandex.net/translate/v2/translate',
json = body,
headers = headers
)
print(response.text)
This example shows how to translate the following two lines of text into Russian: Hello and World. The source language of a text is recognized automatically.
Create a file with the request body, e.g., body.php
:
<?php
$IAM_TOKEN = '<IAM_token>';
$folder_id = '<folder_ID>';
$target_language = 'ru';
$texts = ["Hello", "World"];
$url = 'https://translate.api.cloud.yandex.net/translate/v2/translate';
$headers = [
'Content-Type: application/json',
"Authorization: Bearer $IAM_TOKEN"
];
$post_data = [
"targetLanguageCode" => $target_language,
"texts" => $texts,
"folderId" => $folder_id,
];
$data_json = json_encode($post_data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
?>
Where:
IAM_TOKEN
: IAM token received before starting.folder_id
: Folder ID received before starting.target_language
: Target language. You can get the language code together with a list of supported languages.texts
: Text to translate as an array of strings.
You can save your IAM token to a file. To do this, specify the file path in the token_path
variable:
body.php
<?php
$token_path = '<path_to_file_with_IAM_token>';
$folder_id = '<folder_ID>';
$target_language = 'ru';
$texts = ["Hello", "World"];
$IAM_TOKEN = trim(file_get_contents(realpath($token_path)));
$url = 'https://translate.api.cloud.yandex.net/translate/v2/translate';
$headers = [
'Content-Type: application/json',
"Authorization: Bearer $IAM_TOKEN"
];
$post_data = [
"targetLanguageCode" => $target_language,
"texts" => $texts,
"folderId" => $folder_id,
];
$data_json = json_encode($post_data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
?>