Home  >  Article  >  Backend Development  >  Use go language and Baidu translation API to translate Chinese and Malay into each other

Use go language and Baidu translation API to translate Chinese and Malay into each other

PHPz
PHPzOriginal
2023-08-06 17:12:30629browse

Using Go language and Baidu Translation API to realize mutual translation between Chinese and Malay

1. Introduction

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.

2. Preparation

Before we start, we need to do some preparations.

2.1 Obtain the key of Baidu Translation API

We need to first register a Baidu Translation Open Platform account and create an application to obtain the API key.

2.2 Install the Go language environment

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.

2.3 Install related dependency packages

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

3. Write code

3.1 Import dependency packages and set global variables

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"`
}

3.2 Implement the translation function

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
}

3.3 Test the translation function

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)
}

4. Run the project

4.1 Compile the project

In the terminal, enter the root directory of the project file and execute The following command is used to compile the project:

go build

4.2 Run the project

Execute the following command to run the project:

./project_name

5. Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn