Home  >  Article  >  Backend Development  >  Go language implements Baidu translation API to achieve Chinese-Croatian translation

Go language implements Baidu translation API to achieve Chinese-Croatian translation

王林
王林Original
2023-08-05 20:45:051140browse

go language implements Baidu translation API and realizes Chinese-Croatian translation

With the development of globalization, language communication has become more and more important. In order to meet the demand for instant translation between different languages, machine translation services such as Baidu Translation API emerged as the times require. In this article, we will use Go language to implement Baidu Translation API to realize the translation function from Chinese to Croatian.

First, we need to register a Baidu developer account and create an application to obtain the API Key and Secret Key. Log in to the Baidu Translation Open Platform (http://api.fanyi.baidu.com/) and follow the instructions to complete account registration and application creation. After obtaining the API Key and Secret Key, we can start writing code.

The following is an example of Go language code that implements Baidu Translation API:

package main

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

const (
    translateURL = "http://api.fanyi.baidu.com/api/trans/vip/translate"
    appID        = "your_app_id"
    appKey       = "your_app_key"
    secretKey    = "your_secret_key"
)

func translate(text string) (string, error) {
    salt := strconv.FormatInt(time.Now().Unix(), 10)
    sign := fmt.Sprintf("%s%s%s%s", appID, text, salt, secretKey)
    sign = fmt.Sprintf("%x", md5.Sum([]byte(sign)))
    params := url.Values{}
    params.Set("q", text)
    params.Set("from", "zh")
    params.Set("to", "hr")
    params.Set("appid", appID)
    params.Set("salt", salt)
    params.Set("sign", sign)
    url := fmt.Sprintf("%s?%s", translateURL, params.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 parseResponse(body)
}

func parseResponse(body []byte) (string, error) {
    // 解析百度翻译API的响应结果
    // 这里根据实际情况进行解析,返回翻译后的文本
}

func main() {
    text := "你好,世界!"
    result, err := translate(text)
    if err != nil {
        fmt.Println("翻译失败:", err)
    } else {
        fmt.Println("翻译结果:", result)
    }
}

The translate function in the code is responsible for sending the text to be translated to Baidu Translation API and parsing it The result returned. parseResponseThe function is used to parse the JSON data returned by the API and extract the translation results. In the main function, we pass the Chinese text to be translated into the translate function and print the translation results.

Before running the code, you need to replace appID, appKey and secretKey with the real values ​​you obtained on the Baidu Translation Open Platform .

Through this simple Go code, we implement the translation function from Chinese to Croatian. You can modify the translate function and the parseResponse function as needed to adapt to the translation needs of other language pairs.

Note: This article only implements the most basic translation function. For complex scenarios and special needs, you may need further customization and adjustment. I hope this article can help you get started with Baidu Translation API and Go language development.

The above is the detailed content of Go language implements Baidu translation API to achieve Chinese-Croatian 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