Go SDK
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.
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/alimtTranslate 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:
| Placeholder | Description | Example |
|---|---|---|
<your-access-key-id> | Your AccessKey ID | LTAI5tXxx... |
<your-access-key-secret> | Your AccessKey secret | xXxXxXxx... |
cn-hangzhou | The region where Machine Translation is activated | cn-hangzhou, ap-southeast-1 |
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.goRequest parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
FormatType | string | Yes | Input format of the source text. |
SourceLanguage | string | Yes | Language code of the source text, such as en or zh. |
TargetLanguage | string | Yes | Language code of the translated output, such as en or zh. |
SourceText | string | Yes | The text to translate. |
Scene | string | Yes | The e-commerce scene. |
What's next
Explore other translation APIs in the Machine Translation developer reference.
Learn about AccessKey pair management to secure your credentials.