Home  >  Article  >  Backend Development  >  Go language implements Baidu translation API to realize Chinese and Turkish translation

Go language implements Baidu translation API to realize Chinese and Turkish translation

WBOY
WBOYOriginal
2023-08-04 14:13:48884browse

go language implements Baidu Translation API to realize Chinese-Turkish translation

Baidu Translation API is a commonly used machine translation service that provides translation functions between multiple languages. This article will use the Go language to implement the function of translating Chinese into Turkish through Baidu Translation API. First, we need to apply for a Baidu Translation API account and obtain the corresponding application ID and key. Then, we can use the Go language to write the corresponding code to implement the translation function.

First, we need to introduce the corresponding package and define some variables. The code example is as follows:

package main

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strings"
    "time"
)

const (
    appID  = "your_app_id"     // 替换为自己的应用ID
    appKey = "your_app_key"    // 替换为自己的密钥
    apiUrl = "http://api.fanyi.baidu.com/api/trans/vip/translate" // 翻译API的URL
)

Next, we define a function to generate the signature information required to access Baidu Translation API, code The example is as follows:

func generateSign(query, salt string) string {
    str := appID + query + salt + appKey
    hash := md5.Sum([]byte(str))
    sign := hex.EncodeToString(hash[:])
    return sign
}

Then, we define a function to send an HTTP request and obtain the translation result. The code example is as follows:

func getTranslation(query, from, to string) (string, error) {
    salt := fmt.Sprintf("%d", time.Now().UnixNano())
    sign := generateSign(query, salt)

    values := url.Values{}
    values.Set("q", query)
    values.Set("from", from)
    values.Set("to", to)
    values.Set("appid", appID)
    values.Set("salt", salt)
    values.Set("sign", sign)

    url := apiUrl + "?" + values.Encode()
    resp, err := http.Get(url)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()

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

    return string(body), nil
}

Finally, we can call the getTranslation function in the main function for translation , the code example is as follows:

func main() {
    query := "你好世界" // 需要翻译的文本
    from := "zh"     // 源语言为中文
    to := "tr"       // 目标语言为土耳其文

    result, err := getTranslation(query, from, to)
    if err != nil {
        fmt.Println("翻译出错:", err)
        return
    }

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

Run the above code to translate "Hello World" into Turkish and output the translation results.

Through the above sample code, we can see how to use Go language to implement translation through Baidu Translation API. You only need to replace the application ID and key in the code with your own information, and specify the source language and target language that need to be translated, to realize the translation function between other languages. At the same time, you can also expand and optimize the code according to your actual needs. I hope this article will help you implement the functions of Baidu Translation API!

The above is the detailed content of Go language implements Baidu translation API to realize Chinese and Turkish translation. 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