Writing Baidu Translation API in Go language to implement Chinese-Hindi translation function

PHPz
Release: 2023-08-06 22:24:24
Original
1293 people have browsed it

Writing Baidu Translation API in Go language to implement the Chinese-Hindi translation function

In the current era of globalization, language exchanges have become more frequent and important. As the popularity of Chinese-Hindi language gradually increases, the need to provide Chinese-Hindi translation function is also growing day by day. Baidu Translation API is a powerful tool that can achieve translation between various languages. This article will introduce how to use Go language to write code, call Baidu Translation API, and realize the Chinese-Hindi translation function.

First, we need to apply for a translation API application on the Baidu Developer Platform and obtain the corresponding key. After obtaining the key, we can start writing Go code.

First, we need to introduce some necessary packages:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "strings"
)
Copy after login

Next, we define a function to send an HTTP POST request and return the translated result:

func translate(text string) (string, error) {
    url := "http://api.fanyi.baidu.com/api/trans/vip/translate"

    data := make(map[string]string)
    data["q"] = text
    data["from"] = "auto"
    data["to"] = "zh"

    params := []string{}
    for key, value := range data {
        params = append(params, key+"="+value)
    }

    appid := "your_appid"
    secretKey := "your_secretkey"
    salt := "1435660288"

    sign := appid + text + salt + secretKey
    signMd5 := md5.Sum([]byte(sign))
    signStr := fmt.Sprintf("%x", signMd5)

    params = append(params, "appid="+appid)
    params = append(params, "salt="+salt)
    params = append(params, "sign="+signStr)

    body := strings.NewReader(strings.Join(params, "&"))
    resp, err := http.Post(url, "application/x-www-form-urlencoded", body)
    if err != nil {
        return "", err
    }

    defer resp.Body.Close()
    bb, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", err
    }

    return string(bb), nil
}
Copy after login

In In the above code, we first define the URL of the translation API, and put the text and related parameters that need to be translated into the data dictionary. Then we concatenate the parameters and calculate the signature. Finally, we use the http.Post method to send an HTTP POST request, and parse and return the returned result.

Finally, we can call the translate function in the main function and output the translation result:

func main() {
    text := "Hello, world!"

    result, err := translate(text)
    if err != nil {
        fmt.Println("翻译失败:", err)
        return
    }

    fmt.Println("翻译结果:", result)
}
Copy after login

Save the code as main.go, and then run the program using the go run main.go command to see the output translation results.

Through the above code example, we can realize the translation function between Chinese and Hindi. Of course, it can also be expanded and modified according to your own needs to meet more translation needs.

In short, it is convenient and fast to implement the Chinese-Hindi translation function by writing Baidu Translation API in Go language. I hope this article can provide you with some help, and happy writing!

The above is the detailed content of Writing Baidu Translation API in Go language to implement Chinese-Hindi translation function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!