Detecting languages (Basic)

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 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.

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.


using Google.Cloud.Translation.V2;
using System;

public partial class TranslateSample
{
    public Detection DetectLanguage()
    {
        TranslationClient client = TranslationClient.Create();
        var detection = client.DetectLanguage(text: "Hello world.");
        Console.WriteLine(
            $"{detection.Language}\tConfidence: {detection.Confidence}");
        return detection;
    }
}

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.

import (
	"context"
	"fmt"

	"cloud.google.com/go/translate"
)

func detectLanguage(text string) (*translate.Detection, error) {
	// text := "こんにちは世界"
	ctx := context.Background()
	client, err := translate.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("translate.NewClient: %v", err)
	}
	defer client.Close()
	lang, err := client.DetectLanguage(ctx, []string{text})
	if err != nil {
		return nil, fmt.Errorf("DetectLanguage: %v", err)
	}
	if len(lang) == 0 || len(lang[0]) == 0 {
		return nil, fmt.Errorf("DetectLanguage return value empty")
	}
	return &lang[0][0], nil
}

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.

// TODO(developer): Uncomment these lines.
// import com.google.cloud.translate.*;
// Translate translate = TranslateOptions.getDefaultInstance().getService();

List<String> texts = new LinkedList<>();
texts.add("Hello, World!");
texts.add("¡Hola Mundo!");
List<Detection> detections = translate.detect(texts);

System.out.println("Language(s) detected:");
for (Detection detection : detections) {
  System.out.printf("\t%s\n", detection);
}

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.

// Imports the Google Cloud client library
const {Translate} = require('@google-cloud/translate').v2;

// Creates a client
const translate = new Translate();

/**
 * TODO(developer): Uncomment the following line before running the sample.
 */
// const text = 'The text for which to detect language, e.g. Hello, world!';

// Detects the language. "text" can be a string for detecting the language of
// a single piece of text, or an array of strings for detecting the languages
// of multiple texts.
async function detectLanguage() {
  let [detections] = await translate.detect(text);
  detections = Array.isArray(detections) ? detections : [detections];
  console.log('Detections:');
  detections.forEach(detection => {
    console.log(`${detection.input} => ${detection.language}`);
  });
}

detectLanguage();

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.

use Google\Cloud\Translate\TranslateClient;

/** Uncomment and populate these variables in your code */
// $text = 'The text whose language to detect.  This will be detected as en.';

$translate = new TranslateClient();
$result = $translate->detectLanguage($text);
print("Language code: $result[languageCode]\n");
print("Confidence: $result[confidence]\n");

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.

"""Detects the text's language."""
from google.cloud import translate_v2 as translate

translate_client = translate.Client()

# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.detect_language(text)

print("Text: {}".format(text))
print("Confidence: {}".format(result["confidence"]))
print("Language: {}".format(result["language"]))

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.

# project_id = "Your Google Cloud project ID"
# text       = "The text you would like to detect the language of"

require "google/cloud/translate"

translate = Google::Cloud::Translate.translation_v2_service project_id: project_id
detection = translate.detect text

puts "'#{text}' detected as language: #{detection.language}"
puts "Confidence: #{detection.confidence}"

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.