Detecting Languages

This document describes how to use the Cloud Translation API to detect the language of a string.

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

Protocol

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. The example uses the access token for a service account set up for the project using the Google Cloud Platform Cloud SDK. For instructions on installing the Cloud SDK, setting up a project with a service account, and obtaining an access token, see the Quickstart.

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'q': 'Mi comida favorita es una enchilada.',
}" "https://translation.googleapis.com/language/translate/v2/detect"

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

200 OK
{
  "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 API Quickstart Using Client Libraries . For more information, see the Translation API 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 API Quickstart Using Client Libraries . For more information, see the Translation API Go API reference documentation .

func detectLanguage(text string) (*translate.Detection, error) {
	ctx := context.Background()
	client, err := translate.NewClient(ctx)
	if err != nil {
		return nil, err
	}
	defer client.Close()

	lang, err := client.DetectLanguage(ctx, []string{text})
	if err != nil {
		return nil, err
	}
	return &lang[0][0], nil
}

Java

Before trying this sample, follow the Java setup instructions in the Translation API Quickstart Using Client Libraries . For more information, see the Translation API 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 API Quickstart Using Client Libraries . For more information, see the Translation API Node.js API reference documentation .

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

// 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.
translate
  .detect(text)
  .then(results => {
    let detections = results[0];
    detections = Array.isArray(detections) ? detections : [detections];

    console.log('Detections:');
    detections.forEach(detection => {
      console.log(`${detection.input} => ${detection.language}`);
    });
  })
  .catch(err => {
    console.error('ERROR:', err);
  });

PHP

Before trying this sample, follow the PHP setup instructions in the Translation API Quickstart Using Client Libraries . For more information, see the Translation API PHP API reference documentation .

use Google\Cloud\Translate\TranslateClient;

// $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 API Quickstart Using Client Libraries . For more information, see the Translation API Python API reference documentation .

"""Detects the text's language."""
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 API Quickstart Using Client Libraries . For more information, see the Translation API 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.new project: 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

Protocol

To detect language for more than one string, use the q parameter to specify each string. This example passes two separate strings for detection:

curl -X POST \
     -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
     -H "Content-Type: application/json; charset=utf-8" \
     --data "{
  'q': 'Hello world',
  'q': '我的名字叫傑夫'
}" "https://translation.googleapis.com/language/translate/v2/detect"

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
  "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 example above.

Go

To detect the language of multiple texts, include multiple strings in the slice passed to the Client#DetectLanguage method shown in the example above.

Java

To detect the language of multiple texts, simply pass a list of strings to the Translate#detect method shown in the example above.

Node.js

To detect the language of multiple texts, simply pass an array of strings to the Translate#detect method shown in the example above.

Python

To detect the language of multiple texts, simply pass a list of strings to the Client#detect_language method shown in the example above.

Ruby

To detect the language of multiple texts, simply pass multiple strings to the Translate#detect method shown in the example above.

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Translation API
Need help? Visit our support page.