-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.go
71 lines (59 loc) · 1.52 KB
/
translator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package baidu_translate_sdk
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type BaiduTranslator struct {
appid string // 百度翻译应用 ID
appkey string // 百度翻译应用秘钥
}
func CreateBaiduTranslator(
appid string,
appkey string) *BaiduTranslator {
return &BaiduTranslator{
appid: appid,
appkey: appkey,
}
}
type TranslateResult struct {
From string `json:"from"`
To string `json:"to"`
TransResult []TransResult `json:"trans_result"`
}
type TransResult struct {
Src string `json:"src"`
Dst string `json:"dst"`
}
func (tr *BaiduTranslator) Translate(query string) (string, error) {
t := time.Now()
salt := t.Format("20060102150405")
signInput := fmt.Sprintf("%s%s%s%s", tr.appid, query, salt, tr.appkey)
sign := fmt.Sprintf("%x", md5.Sum([]byte(signInput)))
url := fmt.Sprintf("http://api.fanyi.baidu.com/api/trans/vip/translate?q=%s&from=en&to=zh&appid=%s&salt=%s&sign=%s", url.QueryEscape(query), tr.appid, salt, sign)
response, err := http.Get(url)
if err != nil {
log.Printf("http get 错误:%s", err)
return "", err
}
returnTxt, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("http read 错误:%s", err)
return "", err
}
var result TranslateResult
if err := json.Unmarshal(returnTxt, &result); err != nil {
log.Printf("%s", returnTxt)
return "", err
}
if len(result.TransResult) > 0 {
return result.TransResult[0].Dst, nil
}
return "", errors.New("未知错误")
}