Translating Text with REST

This document describes how to use the common features of the Google Translate API v2 using the RESTful calling style.

Google Translate API is a paid enterprise service for translating large amounts of text. For website translations, we encourage you to use the Google Website Translator gadget.

Before you begin

Before running this sample, take the following steps:
  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform Console project.

    Go to the Projects page

  3. Enable billing for your project.

    Enable billing

  4. Click Continue to enable the API and any related services.
  5. On the Credentials page, get an API key (select Browser key when prompted).
    Note: If you have an existing API key, you can use that key.

Translating text

This section demonstrates a few ways to request translations. To run each example, copy the URL and paste it into your browser. Don't forget to replace YOUR_API_KEY with your actual API key.

The URL for a request has the following format:

https://www.googleapis.com/language/translate/v2?parameters

Three query parameters are required with each translation request:

  • API key: Use the key parameter to identify your application.
  • Target language: Use the target parameter to specify the language you want to translate into.
  • Source text string: Use the q parameter to specify the text to translate.

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

Translating a single string

To translate a string, just make a request using the required parameters. Here is an example that specifies the source language, using the optional source query parameter:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&source=en&target=de&q=Hello%20world

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

200 OK

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt"
            }
        ]
    }
}

In this example, the response is an array containing a single translatedText field with the translation.

Translating multiple strings

To translate more than one string, use the q parameter to specify each one, using an ampersand (&) to delimit each instance. This example shows passing two separate strings for translation:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff

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

200 OK

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt"
            },
            {
                "translatedText": "Mein Name ist Jeff"
            }
        ]
    }
}

Here, translations is an array containing two translatedText fields with the translations, in the same order as the corresponding source strings were provided in the request.

Detecting the source language

If the source parameter is omitted from a request, the API attempts to detect the language of the source text. This example shows such a request:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&target=de&q=Hello%20world

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

200 OK

{
    "data": {
        "translations": [
            {
                "translatedText": "Hallo Welt",
                "detectedSourceLanguage": "en"
            }
        ]
    }
}

Just as in the example above, translations is an array containing a single object with translatedText and the translation of the query. detectedSourceLanguage is a language code. Note that other languages which are not listed in this table may be returned (these will be iso639-1 language codes).

Send feedback about...

Translate API