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:-
Sign in to your Google account.
If you don't already have one, sign up for a new account.
- Select or create a Cloud Platform Console project.
- Enable billing for your project.
- Click Continue to enable the Translate API and any related services.
- 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_KEYis the API key that you retrieved above.sourceis the source language, in this caseenfor English.targetis the target language, in this casedefor 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_KEYis 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:
- 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> - Replace
YOUR_API_KEYwith your actual API key. - Name the text file and save it with an .html extension.
- 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:
- Go to the Cloud Platform Console. Cloud Platform Console
- Select a project, then click Continue.
- In the sidebar on the left, select Credentials.
- Select an API key from the list.
- 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.
Clean up
To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:
- Go to the Cloud Platform Console. Cloud Platform Console
- Click the trash can icon next to the project you want to delete.
What's next
Learn more about the Translate REST API: