Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
© 2022 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. Step-by-step instructions
  2. Detecting language

Detect the language

Written by
Yandex Cloud
  • Before you start
  • Detect the language of a phrase
  • Specify the most likely languages

To detect the language of a text, use the detectLanguage method.

Before you start

To use the examples, install cURL and get the authorization data for your account:

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

Detect the language of a phrase

In this example, we will detect the language that the phrase Hello, world is written in.

To detect the language of the text, pass it in the detectLanguage request body:

$ export FOLDER_ID=b1gvmob95yysaplct532
$ export IAM_TOKEN=CggaATEVAgA...
$ export TEXT="Hello, world"
$ curl -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${IAM_TOKEN}" \
    -d "{\"folderId\": \"${FOLDER_ID}\", \"text\": \"${TEXT}\"}" \
    "https://translate.api.cloud.yandex.net/translate/v2/detect"

The service responds with the language code of the source text. The language code is specified in ISO 639-1 format:

{
    "languageCode": "en"
}

Specify the most likely languages

Some words are spelled the same in different languages. For example, the English word hand is also written as hand in German, Swedish, and Dutch. If the text you transmit contains words like this, Translate may detect the wrong source language.

To avoid mistakes, you can use the languageCodeHints field to specify which languages should be given priority when determining the language of the text:

{
    "folderId": "b1gvmob95yysaplct532",
    "languageCodeHints":["ru", "de"],
    "text": "hand"
}

Save the request body in a file (for example, body.json) and pass it using the detectLanguage method:

$ export IAM_TOKEN=CggaATEVAgA...
$ curl -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${IAM_TOKEN}" \
    -d '@body.json' \
    "https://translate.api.cloud.yandex.net/translate/v2/detect"

{
    "languageCode": "de"
}

Was the article helpful?

Language / Region
© 2022 Yandex.Cloud LLC
In this article:
  • Before you start
  • Detect the language of a phrase
  • Specify the most likely languages