Quickstart (v3beta1)

The Cloud Translation API v3, which is similar to v2 functionality, includes several new features:

See Introducing v3 and Migrating to Translation v3 for details.

This page shows how to make a Cloud Translation API request with curl.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. Set up a GCP Console project.

    Set up a project

    Click to:

    • Create or select a project.
    • Enable the Cloud Translation API for that project.
    • Create a service account.
    • Download a private key as JSON.

    You can view and manage these resources at any time in the GCP Console.

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  4. Install and initialize the Cloud SDK.

Make a Translation API request

Make a Translation API Request using a REST method call to the v3beta1 translate method.

  1. Use curl to make a request to the https://translation.googleapis.com/v3beta1/projects/project-id/locations/global:translateText endpoint. You must include the project-id and indicate location (for example, global or us-central1).

    The curl command includes JSON with the language specified to translate from (source_language_code), the language to translate to (target_language_code), and the text to be translated (contents).

    The source and target languages are identified using the iso-639-1 codes. The source language is English (en) and the target language is Russian (ru).

    The sample curl command uses the gcloud auth application-default print-access-token command to get an authentication token.

  2. Run the following code to perform your first text translation.

    For v2 users, see our Migration guide for differences between v2 and v3beta1

    curl -X POST \
         -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
         -H 'Content-Type: application/json' --data "{
         source_language_code: 'en',
         target_language_code: 'ru',
         contents: ['Dr. Watson, come here!']
    }" "https://translation.googleapis.com/v3beta1/projects/project-id/locations/global:translateText"
    

    You should see a response similar to the following:

    {
      "translations": [
        {
          "translatedText": "Доктор Ватсон, иди сюда!"
        }
      ]
    }
      

Congratulations! You've sent your first request to the Cloud Translation API!

Clean up

To avoid unnecessary Google Cloud Platform charges, use the GCP Console to delete your project if you do not need it.

What's next

هل كانت هذه الصفحة مفيدة؟ يرجى تقييم أدائنا:

إرسال تعليقات حول...

هل تحتاج إلى مساعدة؟ انتقل إلى صفحة الدعم.