Detecting the text language using Translate
To detect the language of a text, use the detectLanguage method.
Note
The detectLanguage method returns the language code of the source text. If the language cannot be detected, the language code field in the response will be empty.
Getting started
To use the examples, install cURL
The examples below are intended to be run in MacOS and Linux. To run them in Windows, see how to work with Bash in Microsoft Windows.
To authenticate under a service account, you can use an API key or an IAM token; to authenticate under a user account, you can only use an IAM token.
Get your account details for authentication in the Translate API:
-
If you do not have a service account, create one.
-
Assign the
ai.translate.user
role for the folder to the service account. -
Get the ID of the folder your service account was created in. Make sure to include the folder ID in the
folderId
field in the body of each request. -
Create an API key with the
yc.ai.translate.execute
scope.Provide the key in the
Authorization
header of each request in the following format:Authorization: Api-Key <API_key>
-
Get the ID of any folder for which your account has the
ai.translate.user
role or higher. Make sure to include the folder ID in thefolderId
field in the body of each request. -
Get an IAM token for your Yandex account, federated account or service account.
Provide the token in the
Authorization
header of each request in the following format:Authorization: Bearer <IAM_token>
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=<folder_ID>
export API_KEY=<API_key>
export TEXT="Hello, world"
curl \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Api-Key ${API_KEY}" \
--data "{\"folderId\": \"${FOLDER_ID}\", \"text\": \"${TEXT}\"}" \
"https://translate.api.cloud.yandex.net/translate/v2/detect"
Where:
FOLDER_ID
: Folder ID you got before you started.-
Where
API_KEY
is the API key you got before you started. If you use an IAM token for authentication, change theAuthorization
header to"Authorization: Bearer <IAM_token>"
.
The service will respond with the language code of the source text:
{
"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 provide contains such words, Translate may detect the wrong source language.
To avoid mistakes, you can use the languageCodeHints
field to specify which languages should be prioritized when determining the text language:
{
"folderId": "<folder_ID>",
"languageCodeHints":["ru", "de"],
"text": "hand"
}
Where:
folderId
: Folder ID you got before you started.languageCodeHints
: Languages to prioritize when determining the language of the text.text
: Text to translate as a string.
Save the request body to a file (e.g., body.json
) and provide the file using the detectLanguage method:
export API_KEY=<API_key>
curl \
--request POST \
--header "Content-Type: application/json" \
--header "Authorization: Api-Key ${API_KEY}" \
--data '@body.json' \
"https://translate.api.cloud.yandex.net/translate/v2/detect"
{
"languageCode": "de"
}
Where API_KEY
is the API key you got before you started. If you use an IAM token for authentication, change the Authorization
header to "Authorization: Bearer <IAM_token>"
.