Go SDK

Updated at:

The Machine Translation Go SDK lets you send translation requests from a Go application using the TranslateECommerce API.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account. Sign up here if you don't have one.

  • Completed real-name verification. Some Alibaba Cloud services are unavailable without it.

  • An AccessKey pair in the Enable state. Get or create one in the RAM console.

Important

Keep your AccessKey ID and AccessKey secret confidential. Never commit them to source control or share them publicly.

Install the SDK

Download the Go SDK:

Add the required package to your Go module:

go get github.com/aliyun/alibaba-cloud-sdk-go/services/alimt

Translate text

The following example translates an e-commerce product title from English to Chinese using the TranslateECommerce API.

package main

import (
	"fmt"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/alimt"
)

func main() {
	// Create an alimt client.
	// Replace <your-access-key-id> and <your-access-key-secret> with your AccessKey pair.
	// Replace cn-hangzhou with the region where Machine Translation is activated.
	alimtClient, err := alimt.NewClientWithAccessKey(
		"cn-hangzhou",              // Region ID
		"<your-access-key-id>",     // AccessKey ID
		"<your-access-key-secret>", // AccessKey secret
	)
	if err != nil {
		panic(err)
	}

	// Build a TranslateECommerce request.
	request := alimt.CreateTranslateECommerceRequest()
	request.Method = "POST"
	request.FormatType = "text"   // Input format: text or html
	request.SourceLanguage = "en" // Source language
	request.TargetLanguage = "zh" // Target language
	request.SourceText = "Hello"  // Text to translate
	request.Scene = "title"       // E-commerce scene: title or description

	// Send the request and print the response.
	response, err := alimtClient.TranslateECommerce(request)
	if err != nil {
		panic(err)
	}
	fmt.Println(response)
}

Replace the following placeholders before running:

PlaceholderDescriptionExample
<your-access-key-id>Your AccessKey IDLTAI5tXxx...
<your-access-key-secret>Your AccessKey secretxXxXxXxx...
cn-hangzhouThe region where Machine Translation is activatedcn-hangzhou, ap-southeast-1
Note

The region ID must match the region where your Machine Translation service is activated. An incorrect region ID causes the request to fail.

Run the program

go run main.go

Request parameters

ParameterTypeRequiredDescription
FormatTypestringYesInput format of the source text.
SourceLanguagestringYesLanguage code of the source text, such as en or zh.
TargetLanguagestringYesLanguage code of the translated output, such as en or zh.
SourceTextstringYesThe text to translate.
ScenestringYesThe e-commerce scene.

What's next