Home  >  Article  >  Backend Development  >  Write Baidu Translation API in Go language to implement Chinese-Danish translation function

Write Baidu Translation API in Go language to implement Chinese-Danish translation function

PHPz
PHPzOriginal
2023-08-07 19:18:16623browse

Write Baidu Translation API in Go language to implement Chinese-Danish translation function

Write Baidu Translation API in Go language to implement Chinese-Danish translation function

Introduction:
With the continuous development of globalization, language translation has become a daily life and an important requirement at work. Baidu Translation API provides a powerful language translation service, which includes translation functions in multiple languages. This article will take Chinese-Danish translation as an example, use the Go language to call the Baidu Translation API to implement the translation function, and give a simple code example.

Background:
Baidu Translation API is a cloud service that enables instant translation between multiple languages. Users only need to pass the text to be translated into the API interface and provide the source language and target language, and the corresponding translation results will be returned.

Code example:
The following is a simple Go language code example that implements the function of translating Chinese into Danish.

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
    "strconv"
    "time"
)

func main() {
    // 百度翻译 API 的 URL 和密钥
    apiURL := "http://api.fanyi.baidu.com/api/trans/vip/translate"
    appKey := "your-app-key"
    appSecret := "your-app-secret"

    // 待翻译的文本
    text := "你好,世界"

    // 源语言和目标语言
    fromLang := "zh"
    toLang := "da"

    // 生成签名然后进行 HTTP 请求
    httpClient := &http.Client{Timeout: 10 * time.Second}
    salt := strconv.FormatInt(time.Now().Unix(), 10)
    sign := appKey + text + salt + appSecret
    postValues := url.Values{}
    postValues.Add("q", text)
    postValues.Add("from", fromLang)
    postValues.Add("to", toLang)
    postValues.Add("appid", appKey)
    postValues.Add("salt", salt)
    postValues.Add("sign", sign)
    resp, err := httpClient.PostForm(apiURL, postValues)
    if err != nil {
        fmt.Println("HTTP 请求失败:", err)
        return
    }
    defer resp.Body.Close()

    // 解析 HTTP 响应,获取翻译结果
    respBody, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("读取响应失败:", err)
        return
    }
    fmt.Println("翻译结果:", string(respBody))
}

Explanation:

  1. First, you need to register an account on the Baidu Translation API platform, create an application, and obtain appKey and appSecret.
  2. The text variable in the code is the text to be translated and can be modified to other text as needed.
  3. fromLang The variable is the source language, the toLang variable is the target language, here they are set to Chinese and Danish respectively.
  4. Next, send a translation request to Baidu Translation API through HTTP POST request. It contains the text to be translated, source language, target language, appKey, salt and sign signature and other parameters.
  5. Finally, parse the HTTP response, obtain the translation result and print it out.

Summary:
Through the above code example, we can see how to call the Baidu Translation API through the Go language to implement the Chinese-Danish translation function. You only need to pass the text to be translated into the API interface and set the source language and target language to obtain the corresponding translation results. This provides us with a simple and practical way to solve cross-language translation needs.

The above is the detailed content of Write Baidu Translation API in Go language to implement Chinese-Danish translation function. 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