Using Go language and Baidu Translation API to realize mutual translation between Chinese and Malay
With the development of globalization, language communication has become more and more important. For developers, a powerful translation tool becomes particularly critical when building multilingual applications. This article will introduce how to use Go language and Baidu Translation API to achieve mutual translation between Chinese and Malay.
Before we start, we need to do some preparations.
We need to first register a Baidu Translation Open Platform account and create an application to obtain the API key.
Make sure you have correctly installed the Go language environment. If it is not installed yet, you can download it from the official website and follow the prompts to install it.
We need to use an HTTP request library to send requests and get responses. Execute the following commands in the terminal to install the corresponding dependency packages:
go get github.com/parnurzeal/gorequest
First, we need to import all Need to depend on packages and set global variables. Create a main.go file in the project file and copy the following content into it:
package main import ( "fmt" "github.com/parnurzeal/gorequest" "encoding/json" ) const ( API_KEY = "你的API密钥" ) type TranslationResponse struct { ErrorCode int `json:"error_code"` ErrorMsg string `json:"error_msg"` TransResult []struct { Src string `json:"src"` Dest string `json:"dst"` } `json:"trans_result"` }
Next, we will implement a simple translation function. Add the following code to the main.go file:
func TranslateText(text, from, to string) (string, error) { url := fmt.Sprintf("https://fanyi-api.baidu.com/api/trans/vip/translate?q=%s&from=%s&to=%s&appid=%s&salt=1435660288&sign=47b8a70a0d9acde1b6734f61e5c4a8e1", text, from, to, API_KEY) request := gorequest.New() resp, _, errs := request.Get(url).End() if errs != nil { return "", errs[0] } var translationResp TranslationResponse decoder := json.NewDecoder(resp.Body) if err := decoder.Decode(&translationResp); err != nil { return "", err } return translationResp.TransResult[0].Dest, nil }
Finally, we will write a simple test function to verify the translation function. Add the following code at the end of the main.go file:
func main() { text := "你好,世界!" from := "zh" to := "ms" translation, err := TranslateText(text, from, to) if err != nil { fmt.Println("翻译失败:", err) return } fmt.Println("翻译结果:", translation) }
In the terminal, enter the root directory of the project file and execute The following command is used to compile the project:
go build
Execute the following command to run the project:
./project_name
By using the Go language and With Baidu Translation API, we have successfully achieved mutual translation between Chinese and Malay. You can expand it as needed to implement translation functions between more languages. Hope this article is helpful to you!
The above is the detailed content of Use go language and Baidu translation API to translate Chinese and Malay into each other. For more information, please follow other related articles on the PHP Chinese website!