This document describes how to use the Cloud Translation - Basic (v2) to detect the language of a string.
Before you begin
Before you can start using the Cloud Translation API, you must have a project that has the Cloud Translation API enabled and the appropriate credentials. You can also install client libraries for common programming languages to help you make calls to the API.
For more information, see the Setup page.
Detecting the language of a text string
You can detect the language of a text string by sending an HTTP request using a URL of the following format:
https://translation.googleapis.com/language/translate/v2/detect
Detecting the language of a single string
REST & CMD LINE
To detect the language of some text, make a POST
request and provide the
appropriate request body. The following shows an example of a POST
request
using curl
or PowerShell. The example uses the access token for a service
account set up for the project using the
Cloud SDK. For instructions on installing
the Cloud SDK, setting up a project with a service account, and obtaining an
access token, see the Setup page.
HTTP method and URL:
POST https://translation.googleapis.com/language/translate/v2/detect
Request JSON body:
{ "q": "Mi comida favorita es una enchilada." }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://translation.googleapis.com/language/translate/v2/detect
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/language/translate/v2/detect " | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "data": { "detections": [ [ { "confidence": 1, "isReliable": false, "language": "es" } ] ] } }
In the response, language
is the detected language code. The other two fields,
isReliable
and confidence
, are deprecated fields included for backward
compatibility; we recommend not basing any decisions or thresholds on their
values.
C#
Before trying this sample, follow the C# setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Translation Quickstart Using Client Libraries. For more information, see the Translation Ruby API reference documentation.
Detecting the language of more than one string
REST & CMD LINE
To detect language for more than one string, use the q
parameter to specify
each string. This example passes two separate strings for detection:
HTTP method and URL:
POST https://translation.googleapis.com/language/translate/v2/detect
Request JSON body:
{ "q": ["Hello world", "我的名字叫傑夫"] }
To send your request, choose one of these options:
curl
Save the request body in a file called request.json
,
and execute the following command:
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
https://translation.googleapis.com/language/translate/v2/detect
PowerShell
Save the request body in a file called request.json
,
and execute the following command:
$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://translation.googleapis.com/language/translate/v2/detect " | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "data": { "detections": [ [ { "confidence": 1, "isReliable": false, "language": "en" } ], [ { "confidence": 1, "isReliable": false, "language": "zh-TW" } ] ] } }
Here, the response contains two detections, in the same order as the corresponding source strings were provided in the request.
C#
To detect the language of multiple texts, simply pass a list of strings to the
client.DetectLanguage
method shown in the preceding example.
Go
To detect the language of multiple texts, include multiple strings in the
slice passed to the Client#DetectLanguage
method shown in the
preceding example.
Java
To detect the language of multiple texts, simply pass a list of strings to the
Translate#detect
method shown in the preceding example.
Node.js
To detect the language of multiple texts, simply pass an array of strings to the
Translate#detect
method shown in the preceding example.
Python
To detect the language of multiple texts, simply pass a list of strings to the
Client#detect_language
method shown in the preceding example.
Ruby
To detect the language of multiple texts, simply pass multiple strings to the
Translate#detect
method shown in the preceding example.