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 :

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

Next Steps