Use go language and Baidu translation API to achieve Chinese-Slovak translation

WBOY
Release: 2023-08-05 10:03:20
Original
1185 people have browsed it

Using Go language and Baidu Translation API to realize Chinese-Slovak translation

With the deepening of global economic and cultural exchanges, the demand for translation tools is increasing. In the era of mobile Internet, various translation applications emerge in endlessly. Among them, Baidu Translation API is a popular translation service that provides translation functions for multi-lingual texts. This article will introduce how to use Go language and Baidu Translation API to achieve Chinese-Slovak translation.

First, we need to obtain the developer key of Baidu Translation API. Register an account in Baidu Developer Center and create an application, and then obtain the API Key and Secret Key.

Next, we need to write Go language code to request Baidu Translation API for translation. First, we need to introduce the http package and crypto/md5 package of the Go language. The code is as follows:

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strconv"
    "time"
)
Copy after login

Next, we need to define some necessary constants, such as the URL of the Baidu Translation API and the developer key.

const (
    apiUrl       = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    appid        = "[你的百度开发者应用APPID]"
    appKey       = "[你的百度开发者应用API Key]"
    appSecretKey = "[你的百度开发者应用Secret Key]"
)
Copy after login

Then, we need to define a function to generate the signature of the request for authentication.

func buildSign(query string, salt string) string {
    sign := appid + query + salt + appSecretKey
    h := md5.New()
    h.Write([]byte(sign))
    return hex.EncodeToString(h.Sum(nil))
}
Copy after login

Next, we need to define a function to send an HTTP request and get the translation results.

func translate(query string) (string, error) {
    httpClient := http.Client{
        Timeout: time.Second * 5,
    }

    salt := strconv.FormatInt(time.Now().Unix(), 10)
    sign := buildSign(query, salt)

    data := url.Values{}
    data.Set("q", query)
    data.Set("from", "zh")
    data.Set("to", "sk")
    data.Set("appid", appid)
    data.Set("salt", salt)
    data.Set("sign", sign)

    url := apiUrl + "?" + data.Encode()

    req, err := http.NewRequest(http.MethodGet, url, nil)
    if err != nil {
        return "", err
    }

    req.Header.Set("Content-Type", "application/json")

    res, getErr := httpClient.Do(req)
    if getErr != nil {
        return "", getErr
    }
    defer res.Body.Close()

    body, readErr := ioutil.ReadAll(res.Body)
    if readErr != nil {
        return "", readErr
    }

    return string(body), nil
}
Copy after login

Finally, we can write a main function to test our translation function.

func main() {
    translation, err := translate("你好")
    if err != nil {
        fmt.Println("翻译出错:", err)
        return
    }
    fmt.Println(translation)
}
Copy after login

Through the above code, we can use the Go language and Baidu Translation API to achieve Chinese-Slovak translation. In the main function, we call the translate function and pass in the text to be translated as a parameter to obtain the translation result.

The above code is just a simple example to implement the Chinese-Slovak translation function. In practical applications, we can further encapsulate the code and add functions such as exception handling and error checking to improve stability and reliability.

In short, using Go language and Baidu Translation API to achieve Chinese-Slovak translation is a very useful and practical technology. In this way, we can break down language barriers and promote communication and cooperation between different cultures. I hope this article is helpful to people who are learning or using Go language for translation development.

The above is the detailed content of Use go language and Baidu translation API to achieve Chinese-Slovak translation. 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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template