Detecting Languages

This document describes how to use the Google 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

Two query parameters are required with each detection request:

  • Source text string: Use the q parameter to specify the text to translate.
  • API key: Use the key parameter to identify your application. If you are using OAuth 2.0 service account credentials (recommended), do not supply this parameter.

All other query parameters are optional. The URL for the GET request, including parameters, must be less than 2K characters.

Detecting the language of a single string

Protocol

Here is an example HTTP POST request that detects the language of the sentence "Google Translate Rocks":

POST https://translation.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY
{
  'q': 'Google Translate Rocks',
}

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": 0.24697750806808472,
          "isReliable": false,
          "language": "en"
        }
      ]
    ]
  }
}

In this example, language is the detected language code. The isReliable member has been deprecated. The confidence field is an optional floating point value between 0 and 1. The closer this value is to 1, the higher the confidence level for the language detection. The confidence field is not always available.

C#

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

static object DetectLanguage(string text)
{
    TranslationClient client = TranslationClient.Create();
    var detection = client.DetectLanguage(text);
    Console.WriteLine("{0}\tConfidence: {1}",
        detection.Language, detection.Confidence);
    return 0;
}

Go

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

public static void detectLanguage(String sourceText, PrintStream out) {
  Translate translate = createTranslateService();
  List<Detection> detections = translate.detect(ImmutableList.of(sourceText));
  System.out.println("Language(s) detected:");
  for (Detection detection : detections) {
    out.printf("\t%s\n", detection);
  }
}

Node.js

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

// Instantiates a client
const translate = Translate();

// The text for which to detect language, e.g. "Hello, world!"
// const text = '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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

def detect_language(text):
    """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

For more on installing and creating a Cloud Translation API client, refer to Cloud Translation API Client Libraries.

# 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:

POST https://translation.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY
{
  'q': 'Hello world',
  'q': '我的名字叫傑夫'
}

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

{
  "data": {
    "detections": [
      [
        {
          "confidence": 0.71052581071853638,
          "isReliable": false,
          "language": "en"
        }
      ],
      [
        {
          "confidence": 0.87668424844741821,
          "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.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Google Cloud Translation API Documentation