Translate text using the Cloud Translation API

Makes an HTTP POST request to the Cloud Translation API to translate text from English to Russian.

Code sample

YAML

main:
  params: [args]
  steps:
    - new_translation:
        call: http.post
        args:
          url: ${"https://translation.googleapis.com/v3/projects/"+sys.get_env("GOOGLE_CLOUD_PROJECT_NUMBER")+":translateText"}
          auth:
            type: OAuth2
          body:
            contents: ${args.textToTranslate}
            sourceLanguageCode: "en-US"
            targetLanguageCode: "ru-RU"
        result: translate_response
    - assign_translation:
        assign:
          - translation_result: ${translate_response.body.translations[0].translatedText}
    - return_result:
        return: ${translation_result}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.