Detect the language
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:
- On the billing page, make sure that your billing account status is
ACTIVE
orTRIAL_ACTIVE
. If you don't have a billing account, create one. - Get an IAM token required for authentication.
- Get the ID of any folder that your account is granted the
editor
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
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.
-
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
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"
}