Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex Translate
  • Getting started
  • Step-by-step instructions
    • All instructions
    • How to translate text
    • How to improve the accuracy of translations
    • Detecting language
    • Getting a list of supported languages
  • Concepts
    • Overview
    • Retraining models
    • Supported languages
    • Glossaries
    • Glossary support
  • API reference
    • Authentication in the API
    • gRPC
      • Overview
      • TranslationService
    • REST
      • v2
        • Handling errors
        • Troubleshooting
        • Overview
        • Translation
          • Overview
          • detectLanguage
          • listLanguages
          • translate
      • v1 (DEPRECATED)
        • Overview
        • How to translate text
        • Detecting language
        • List of supported languages
        • Response format
  • Quotas and limits
  • Access management
  • Pricing policy
    • Current pricing policy
    • Archive
      • Policy before January 1, 2019
  • Questions and answers
  1. Getting started

Getting started with Translate

Written by
Yandex Cloud
  • Before you start
  • Translate text from any language

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:

User's Yandex account
Service account
Federated account
  1. On the billing page, make sure that the payment account is in ACTIVE or TRIAL_ACTIVE status. If you don't have a billing account, create one.
  2. Get an IAM token, which is required for authentication.
  3. Get the ID of any folder that your account is granted the ai.translate.user role or higher for.
  1. 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>
      
  2. 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.

  1. Authenticate with the CLI as a federated user.

  2. Use the CLI to get an IAM token required for authentication:

    yc iam create-token
    
  3. 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:

CLI
Python
PHP

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.

  1. 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.
  2. 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.

What's next

  • Learn more about service concepts
  • Read our other instructions
  • Learn about API authentication methods

Was the article helpful?

Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
In this article:
  • Before you start
  • Translate text from any language