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 GET request using a URL of the following format:

https://translation.googleapis.com/language/translate/v2/detect?*parameters*

Two query parameters are required with each detection request:

  • API key: Use the key parameter to identify your application.
  • Source text string: Use the q parameter to specify the text to translate.

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 that detects the language of the sentence "Google Translate Rocks":

https://translation.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY&q=Google%20Translate%20Rocks

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

200 OK
{
  "data": {
    "detections": [
      [
        {
          "language": "en",
          "isReliable": false,
          "confidence": 0.18397073
        }
      ]
    ]
  }
}

In this example, language is the detected language code. The isReliable member has been deprecated. The confidence member 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. Note that this member is no always available.

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.

function detectLanguage (input) {
  // Instantiates a client
  const translate = Translate();

  // Detects the language. "input" 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.
  return translate.detect(input)
    .then((results) => {
      let detections = results[0];

      if (!Array.isArray(detections)) {
        detections = [detections];
      }

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

      return detections;
    });
}

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 one, using an ampersand (&) to delimit each instance. This example passes two separate strings for detection:

https://translation.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY&q=Hello%20world&q=%E6%88%91%E5%A7%93%E8%AD%9A

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

{
  "data": {
    "detections": [
      [
        {
          "language": "en",
          "isReliable": false,
          "confidence": 0.114892714
        }
      ]
    ]
  },
  "data": {
    "detections": [
      [
        {
          "language": "zh-TW",
          "isReliable": false,
          "confidence": 0.7392313
        }
      ]
    ]
  }
}

Here, the response contains two detections, in the same order as the corresponding source strings were provided in the request.

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.

Send feedback about...

Google Cloud Translation API Documentation