Quickstart

This quickstart shows you how to use the Google Translate API v2 to translate a piece of text, and to detect the source language for a piece of text.

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

Translate text

To translate the phrase hello world, enter the following string in your browser address bar:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=hello%20world&source=en&target=de
where:
  • YOUR_API_KEY is the API key that you retrieved above.
  • source is the source language, in this case en for English.
  • target is the target language, in this case de for German.

The JSON response contains the translated text in German:

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

Detect source language

To detect the language of a piece of text, enter the following string in your browser address bar:

https://www.googleapis.com/language/translate/v2/detect?key=YOUR_API_KEY&q=google+translate+is+fast
where:
  • YOUR_API_KEY is the API key that you retrieved above.

The JSON response contains the detected language as well as a confidence rating.

{
 "data": {
  "detections": [
   [
    {
     "language": "en",
     "isReliable": false,
     "confidence": 0.18006317
    }
   ]
  ]
 }
}

Translate text using JavaScript

To translate text using JavaScript:

  1. Copy the following JavaScript code, and paste it into a new text file.
    <html>
      <head>
        <title>Translate API Example</title>
      </head>
      <body>
        <div id="sourceText">Hello world</div>
        <div id="translation"></div>
        <script>
          function translateText(response) {
            document.getElementById("translation").innerHTML += "<br>" + response.data.translations[0].translatedText;
          }
        </script>
        <script>
          var newScript = document.createElement('script');
          newScript.type = 'text/javascript';
          var sourceText = escape(document.getElementById("sourceText").innerHTML);
          // WARNING: Your API key will be visible in the page source.
          // To prevent misuse, restrict your key to designated domains or use a
          // proxy to hide your key.
          var source = 'https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&source=en&target=de&callback=translateText&q=' + sourceText;
          newScript.src = source;
    
          document.getElementsByTagName('head')[0].appendChild(newScript);
        </script>
      </body>
    </html>
    
  2. Replace YOUR_API_KEY with your actual API key.
  3. Name the text file and save it with an .html extension.
  4. Open the file in a web browser.

The web browser displays the source text and the translation:

   Hello world

   Hallo Welt
  

Secure your API key

if you use the Translate API from JavaScript, your API key is viewable in the HTML source for your page. To prevent unauthorized use, restrict the use of your key to only domains you administer:

  1. Go to the Cloud Platform Console.
  2. Cloud Platform Console
  3. Select a project, then click Continue.
  4. In the sidebar on the left, select Credentials.
  5. Select an API key from the list.
  6. Under Accept requests from these HTTP referrers (web sites), add a domain and click Save You can add more than one domain to the list.
For more information, see Best practices for securely using API keys.

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Go to the Cloud Platform Console.
  2. Cloud Platform Console
  3. Click the trash can icon next to the project you want to delete.

What's next

Learn more about the Translate REST API:

Send feedback about...

Translate API