Translating text

This page shows you how to translate sample text with both the Basic and Advanced editions of Cloud Translation. They give you access to the standard Neural Machine Translation (NMT) model and the Translation LLM, which is our latest LLM-style translation model.

The Cloud Translation - Basic API provides straightforward, immediate access to these models. Cloud Translation - Advanced, on the other hand, is optimized for customization and long-form content use cases. For sample code, see the Advanced text translation example. For special situations, you can also use Cloud Translation - Advanced to create custom models.

Cloud Translation - Advanced also provides advanced text translation capabilities like translating documents and creating glossaries to ensure that your domain-specific terminology is translated correctly.

Before you begin

Before you can start using the Cloud Translation API, you should complete the Cloud Translation setup page, which includes creating a project, enabling the Cloud Translation API, and setting up authentication. The setup page also provides instructions for installing client libraries for common programming languages, which is optional.

Translation LLM with Cloud Translation - Basic API

To use the standard Translation LLM model in Cloud Translation - Basic API, pass its full resource name in the model parameter.

curl -X POST \
     -H "Content-Type: application/json; charset=utf-8" \
     -d '{
       "q": ["The old lighthouse stood on the edge of the cliff."],
       "target": "es",
       "model": "projects/PROJECT_ID/locations/REGION_NAME/models/general/translation-llm"
     }' \
     "https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY"

The Cloud Translation - Basic API supports only standard NMT and standard LLM models, not customized models. To use a customized model, or other advanced features like document translation or glossaries, use Cloud Translation - Advanced. For more information on calling the Basic API, see Basic text translation example.

Advanced text translation

For translations with Cloud Translation - Advanced, the input can be plain text or HTML. Cloud Translation API doesn't translate any HTML tags in the input, only text that appears between the tags. The output retains the (untranslated) HTML tags, with the translated text between the tags to the extent possible due to differences between the source and target languages.

Advanced text translation example

REST

To translate text, make a POST request and provide JSON in the request body that identifies the language to translate from (source_language_code), the language to translate to (target_language_code), and the text to translate (contents). You can provide multiple strings of text to translate by including them in your JSON (see example). You identify your source and target languages by using their ISO-639 codes.

The following shows an example of a POST request using curl or PowerShell. The example uses the access token for a service account set up for the project using the Google Cloud Google Cloud CLI. For instructions on installing the Google Cloud CLI, setting up a project with a service account, and obtaining an access token, see the Setup page.

Before using any of the request data, make the following replacements:

  • PROJECT_NUMBER_OR_ID: the numeric or alphanumeric ID of your Google Cloud project

HTTP method and URL:

POST https://translation.googleapis.com/v3/projects/PROJECT_NUMBER_OR_ID:translateText

Request JSON body:

{
  "sourceLanguageCode": "en",
  "targetLanguageCode": "ru",
  "contents": ["Dr. Watson, come here!", "Bring me some coffee!"]
}

To send your request, expand one of these options:

You should receive a JSON response similar to the following:

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

The translations array contains two translatedText fields with translations provided in the requested targetLanguageCode language (ru: Russian). The translations are listed in the same order as the corresponding source array in the request.

Go

Before trying this sample, follow the Go setup instructions in the Cloud Translation quickstart using client libraries. For more information, see the Cloud Translation Go API reference documentation.

To authenticate to Cloud Translation, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Imports the Google Cloud Translation library
import (
	"context"
	"fmt"
	"io"

	translate "cloud.google.com/go/translate/apiv3"
	"cloud.google.com/go/translate/apiv3/translatepb"
)

func translateText(w io.Writer, projectID string, sourceLang string, targetLang string, text string) error {
	// projectID := "your-project-id"
	// sourceLang := "en-US"
	// targetLang := "fr"
	// text := "Text you wish to translate"

	// Instantiates a client
	ctx := context.Background()
	client, err := translate.NewTranslationClient(ctx)
	if err != nil {
		return fmt.Errorf("NewTranslationClient: %w", err)
	}
	defer client.Close()

	// Construct request
	req := &translatepb.TranslateTextRequest{
		Parent:             fmt.Sprintf("projects/%s/locations/global", projectID),
		SourceLanguageCode: sourceLang,
		TargetLanguageCode: targetLang,
		MimeType: