Getting started with Translate
In this section, you will learn how to translate text using the Translate API. To translate a text, pass it using the translate method.
Before you start
To use the examples, install cURL.
Get the authorization data for your account:
- On the billing page, make sure that the payment account is in
ACTIVE
orTRIAL_ACTIVE
status. If you don't have a billing account, create one. - Get an IAM token, which is required for authentication.
- Get the ID of any folder that your account is granted the
ai.translate.user
role or higher for.
-
Select the authentication method:
-
Get an IAM token used in the examples.
-
Create an API key. Pass 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 a higher role for the folder where it was created.Don't specify the folder ID in your requests: the service uses the folder where the service account was created.
-
Use the CLI to get an IAM token required for authentication:
yc iam create-token
-
Get the ID of any folder that your account is granted the
ai.translate.user
role or higher for.
Translate text from any language
To translate a text, pass it using the translate method:
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.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 in ISO 639-1 format. You can get the language code with a list of supported languages.
-
To upload the file to translate, run the command:
export IAM_TOKEN=CggaATEVAgA... curl -X POST \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${IAM_TOKEN}" \ -d '@<path_to_json_file>' \ "https://translate.api.cloud.yandex.net/translate/v2/translate"
Where
IAM_TOKEN
: IAM token received before starting.The response from the service will contain translated text:
{ "translations": [ { "text": "Привет", "detectedLanguageCode": "en" }, { "text": "Мир", "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-токен>'
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:
folder_id
: Folder ID received before starting.texts
: Text to translate as a list of strings.target_language
: Target language in ISO 639-1 format. You can get the language code with a list of supported languages.IAM_TOKEN
: IAM token received before starting.
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.php
).
$IAM_TOKEN = '<IAM-токен>';
$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:
folder_id
: Folder ID received before starting.texts
: Text to translate as a list of strings.target_language
: Target language in ISO 639-1 format. You can get the language code with a list of supported languages.IAM_TOKEN
: IAM token received before starting.