Using go language and Baidu translation API to translate Chinese and Romanian into each other

王林
Release: 2023-08-05 23:29:21
Original
496 people have browsed it

Using Golang and Baidu Translation API to achieve mutual translation between Chinese and Romanian

Introduction

This article will introduce how to use Go language and Baidu Translation API to realize mutual translation between Chinese and Romanian. We will use Baidu Translation API for text translation and use Go language to write a simple program to implement the translation function.

Baidu Translation API

Baidu Translation API is an open interface that provides mutual translation between multiple languages. By sending an HTTP request to the API, the translation results of the text can be obtained. Before we start, we need to apply for a developer account for Baidu Translation API and obtain the API's application ID and key. For the specific application process, please refer to the official documentation of Baidu Translation API.

Program Implementation

First, we need to import the HTTP package and JSON package we need to use.

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "net/url"
)
Copy after login

Then, we define a function to implement the text translation function.

func translate(text, from, to string) (string, error) {
    appID := "your_app_id"     // 替换为你的应用ID
    appKey := "your_app_key"   // 替换为你的应用密钥
    apiURL := "http://api.fanyi.baidu.com/api/trans/vip/translate"

    // 构造请求URL
    params := url.Values{}
    params.Add("q", text)
    params.Add("from", from)
    params.Add("to", to)
    params.Add("appid", appID)
    params.Add("salt", "123456")  // 为了简化,我们直接指定salt
    sign := appID + text + "123456" + appKey
    sign = md5.Sum([]byte(sign))
    params.Add("sign", sign)

    // 发送HTTP请求
    resp, err := http.Get(apiURL + "?" + params.Encode())
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()

    // 读取响应内容
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", err
    }

    // 解析JSON响应
    var result struct {
        TransResult []struct {
            Src string `json:"src"`
            Dst string `json:"dst"`
        } `json:"trans_result"`
    }
    err = json.Unmarshal(body, &result)
    if err != nil {
        return "", err
    }

    // 返回翻译结果
    return result.TransResult[0].Dst, nil
}
Copy after login

Finally, we write a main function to test the translation function.

func main() {
    // 测试文本
    text := "你好,世界!"

    // 翻译
    translated, err := translate(text, "zh", "ro")
    if err != nil {
        fmt.Println("翻译失败:", err)
        return
    }

    // 打印翻译结果
    fmt.Println(translated)
}
Copy after login

Run the program

Save the above code as a go file, and then compile and run the program through the command line. You will see the program output Chinese Romanian translation results.

$ go run main.go
Salut, lume!
Copy after login

Conclusion

By using Golang and Baidu Translation API, we successfully realized the mutual translation function between Chinese and Romanian. This sample code can serve as a starting point that you can further extend and customize to your needs. I hope this article will help you learn Go language and Baidu Translation API!

The above is the detailed content of Using go language and Baidu translation API to translate Chinese and Romanian into each other. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!