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

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

王林
Release: 2023-08-04 16:04:43
Original
1164 people have browsed it

Writing Baidu Translation API in Go language to implement the Chinese-Vietnamese translation function

In today's era of globalization, cross-language communication has become more and more important. As a powerful language translation tool, Baidu Translation API provides developers with a convenient interface, allowing us to easily implement translation functions between various languages. This article will introduce how to use Go language to write code and realize Chinese-Vietnamese translation through Baidu Translation API.

Step 1: Apply for a Baidu Translation API account

Before we begin, we need to apply for a Baidu Translation API account and obtain the API Key and Secret Key. Open the official website of Baidu Translation Open Platform (https://api.fanyi.baidu.com/), click the "Register/Login" button, and register or log in on the login page. After successfully logging in, click the avatar in the upper right corner, select "Console", find "My Application", click "Create Application", fill in the application name and other information, and obtain the API Key and Secret Key.

Step 2: Install the necessary libraries

Before writing code in the go language, we need to install and introduce some necessary libraries.

First, we need to install the HTTP library of go language:

go get -u github.com/levigross/grequests
Copy after login

Secondly, we need to install the SDK library of Baidu Translation API:

go get -u github.com/baidu/go-lib/baidu/aip
Copy after login

Step 3: Write code

The following is a simple go language code example to implement the Chinese-Vietnamese translation function through Baidu Translation API:

package main

import (
    "fmt"
    "net/url"

    "github.com/levigross/grequests"
    "github.com/baidu/go-lib/baidu/aip"
)

const (
    apiKey = "Your API Key"
    secretKey = "Your Secret Key"
    translateUrl = "https://fanyi-api.baidu.com/api/trans/vip/translate"
)

func Translate(text string, fromLang string, toLang string) (string, error) {
    client := aip.NewAipSpeech(apiKey, secretKey)
    params := url.Values{}
    params.Set("q", text)
    params.Set("from", fromLang)
    params.Set("to", toLang)
    params.Set("appid", "20151113000005349")
    salt := aip.GetRandomString(8)
    params.Set("salt", salt)
    sign := aip.GetMd5String(apiKey + text + salt + secretKey)
    params.Set("sign", sign)

    resp, err := grequests.Get(translateUrl, &grequests.RequestOptions{Params: params})
    if err != nil {
        return "", err
    }

    fmt.Println(resp.String())

    return "", nil
}

func main() {
    text := "你好"
    fromLang := "zh"
    toLang := "vie"

    result, err := Translate(text, fromLang, toLang)
    if err != nil {
        fmt.Println("翻译失败:", err)
    } else {
        fmt.Println("翻译结果:", result)
    }
}
Copy after login

In the above code, we first introduced the required libraries, and then defined the constants And the translation function Translate. In the translation function, we call the Baidu Translation API interface and pass in parameters such as the text to be translated, the source language, and the target language. Finally, we send the translation request through the HTTP library and obtain the translation results.

In the main function, we define a text to be translated, the source language is Chinese, and the target language is Vietnamese. Then, we call the translation function and print out the translation results.

Step 4: Run the code

After completing the code writing, we can run the code as follows:

go run main.go
Copy after login

If everything goes well, you will see something similar to the following Output:

{
    "from": "zh",
    "to": "vie",
    "trans_result": [
        {
            "src": "你好",
            "dst": "Xin chào"
        }
    ]
}
翻译结果: Xin chào
Copy after login

The above is the entire process of writing Baidu Translation API in go language to realize the Chinese-Vietnamese translation function. Through this simple example, we can see that it is very simple to implement cross-language translation function using Baidu Translation API and Go language. Whether in daily life or development work, such functions can help us better communicate and collaborate across languages.

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