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

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

PHPz
PHPzOriginal
2023-08-05 16:39:171495browse

Go language implements Baidu Translation API to realize Chinese-Australian translation

Introduction:
With the development of the Internet, communication between the world has become more and more frequent. Among them, translation services play an important role in promoting communication between different languages. In this article, we will introduce how to use Go language to implement Baidu Translation API to achieve translation between Chinese and Australian.

Prerequisites:
In order to start using Baidu Translation API, you need the following prerequisites:

  1. Register a Baidu developer account and create a translation application
  2. Get the APP ID and key of Baidu Translation API

Code example:
The following is a sample code using Go language to implement Baidu Translation API:

package main

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

func main() {
    appid := "your_appid"  // 替换为您自己的APP ID
    secret := "your_secret"  // 替换为您自己的密钥

    from := "zh"  // 要翻译的文本的语言,这里是中文
    to := "en"  // 目标语言,这里是英文
    text := "你好,世界!"  // 要翻译的文本

    salt := strconv.FormatInt(time.Now().Unix(), 10)
    sign := md5.Sum([]byte(appid + text + salt + secret))
    signature := hex.EncodeToString(sign[:])

    url := "https://fanyi-api.baidu.com/api/trans/vip/translate?q=" + url.QueryEscape(text) + "&from=" + from + "&to=" + to + "&appid=" + appid + "&salt=" + salt + "&sign=" + signature

    response, err := http.Get(url)
    if err != nil {
        fmt.Println("翻译请求失败:", err)
        return
    }
    defer response.Body.Close()

    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("翻译请求失败:", err)
        return
    }

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

Analysis:
In the above code, first replace appid and secret with your own APP ID and key. Then, the source language to be translated (from), the target language (to), and the text to be translated (text) are specified.

Next, a random salt is generated as one of the parameters, and the MD5 algorithm is used to appid, text, salt and secret were signed and sign was generated.

Finally, the requested URL is constructed and an HTTP GET request is sent. Output the translation results in string form.

Conclusion:
By using the Go language to implement the Baidu Translation API, we can easily translate between Chinese and Australian. This sample code can serve as the basis for your own translation application, which you can modify and extend to suit your needs. Not only that, you can also use other functions of Baidu Translation API, such as speech synthesis, language detection, etc. I hope this article has been helpful for you and made it easier for you to communicate around the world!

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