This document describes how to use the Cloud Translation - Advanced 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/v3/projects/project-number-or-id/locations/global:detectLanguage
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
and PowerShell. The example uses the access token for a service account set up for the
project using the Google Cloud 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.
Before using any of the request data below, make the following replacements:
- project-number-or-id: your Google Cloud project number or ID
HTTP method and URL:
POST https://translation.googleapis.com/v3/projects/project-number-or-id/locations/global:detectLanguage
Request JSON body:
{ "content":"Доктор Ватсон, иди сюда!" }
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/v3/projects/project-number-or-id/locations/global:detectLanguage
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/v3/projects/project-number-or-id/locations/global:detectLanguage " | Select-Object -Expand Content
You should receive a JSON response similar to the following:
{ "languages": [ { "languageCode": "ru", "confidence": 1 } ] }
languageCode
provides the language code of the language
detected. confidence
is a range from 0 to 1. 1 is 100% confident.
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.