The Cloud Translation allows you to translate an arbitrary string into any supported language. Language detection is also available in cases where the source language is unknown.
What you'll learn
- Creating a Cloud Translation API request and calling the API with curl
- Translating Text
- Using the Premium Edition
- Detecting Language
What you'll need
Survey
How will you use this tutorial?
How would rate your experience with Google Cloud Platform?
Self-paced environment setup
If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:
Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!).
New users of Google Cloud Platform are eligible for a $300 free trial.
Codelab-at-a-conference setup
The instructor will be sharing with you temporary accounts with existing projects that are already setup so you do not need to worry about enabling billing or any cost associated with running this codelab. Note that all these accounts will be disabled soon after the codelab is over.
Once you have received a temporary username / password to login from the instructor, log into the Google Cloud Console: https://console.cloud.google.com/.
Here's what you should see once logged in :
Click on the menu icon in the top left of the screen.
Select API Manager from the drop down.
Click on Enable API.
Then, search for "translate" in the search box. Click on Google Cloud Translation API:
If the API is already enabled, you will see a "Disable" button. Do not disable the API.
If the API is disabled, Click Enable to enable the Cloud Translation API:
Wait for a few seconds for it to enable. You will see this once it's enabled:
Google Cloud Shell is a command line environment running in the Cloud. This Debian-based virtual machine is loaded with all the development tools you'll need (gcloud
, bq
, git
and others) and offers a persistent 5GB home directory. We'll use Cloud Shell to create our request to the Translation API.
To get started with Cloud Shell, Click on the "Activate Google Cloud Shell" icon in top right hand corner of the header bar
A Cloud Shell session opens inside a new frame at the bottom of the console and displays a command-line prompt. Wait until the user@project:~$ prompt appears
Since we'll be using curl to send a request to the Translation API, we'll need to generate an API key to pass in our request URL. To create an API key, navigate to the API Manager section of your project dashboard:
Then, navigate to the Credentials tab and click Create credentials:
In the drop down menu, select API key:
Next, copy the key you just generated.
Copy your API Key to your clipboard then save it to an environment variable in Cloud Shell using the following line of code. Be sure to replace YOUR_API_KEY with the key from your clipboard.
export API_KEY=YOUR_API_KEY
In this example you will translate the string "My name is Steve" into Spanish. Pass the text to be translated, along with the API key environment variable you saved earlier, to the Translation API with the following curl command:
TEXT="My%20name%20is%20Steve"
curl "https://translation.googleapis.com/language/translate/v2?target=es&key=${API_KEY}&q=${TEXT}"
Your response should look like the following:
{
"data": {
"translations": [
{
"translatedText": "Mi nombre es Steve",
"detectedSourceLanguage": "en"
}
]
}
}
In the response, you can see that the translated text as well as the source language that the API detected.
In addition to translating text, the Translation API also lets you detect the language of text. In this example you will detect the language of two strings. Pass the text to be examined, along with the API key environment variable you saved earlier, to the Translation API with the following curl command:
TEXT_ONE="Meu%20nome%20é%20Steven"
TEXT_TWO="日本のグーグルのオフィスは、東京の六本木ヒルズにあります"
curl "https://translation.googleapis.com/language/translate/v2/detect?key=${API_KEY}&q=${TEXT_ONE}&q=${TEXT_TWO}"
Your response should look like this:
{
"data": {
"detections": [
[
{
"confidence": 0.20671661198139191,
"isReliable": false,
"language": "pt"
}
],
[
{
"confidence": 0.97750955820083618,
"isReliable": false,
"language": "ja"
}
]
]
}
}
The languages returned by this sample are "pt" and "ja". These are the ISO-639-1 identifiers for Portuguese and Japanese. This list of languages supported by the Translation API lists all the possible language codes which can be returned.
You've learned how to translate text with the Cloud Translation API!
What we've covered
- Creating a Cloud Translation API request and calling the API with curl
- Translating Text
- Using the Premium Edition
- Detecting Language
Next Steps
- Check out the Translation API sample applications built using client libraries using a variety of popular programming languages.
- Try out the Vision API and Speech API!