Skip to content

Commit 2c8b5aa

Browse files
author
UlinoyaPed
committed
重做翻译函数
加入url encode,(应该)不会出现错误 Signed-off-by: UlinoyaPed <l15010104769@outlook.com>
1 parent 37b4987 commit 2c8b5aa

File tree

6 files changed

+190
-115
lines changed

6 files changed

+190
-115
lines changed

.gitignore

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
1-
.idea
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
#releases
18+
releases/*
19+
*.syso
20+
21+
#build
22+
!go-winres.exe
23+
!buildcross.bat
24+
!winres/*
25+
26+
#other
27+
*.log
28+
*.bk

README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,31 @@ go install github.com/UlinoyaPed/BaiduTranslate
1010

1111
## 使用
1212

13-
BaiduInfo结构体记录配置项,Salt为数据传送时加盐(库中已有函数实现,可直接调用),From项为翻译源语言(auto自动识别),To项为译文语言
14-
15-
```golang
13+
```go
1614
package main
1715

1816
import (
1917
"fmt"
18+
2019
"github.com/UlinoyaPed/BaiduTranslate"
2120
)
2221

2322
func main() {
24-
bi := BaiduTranslate.BaiduInfo{AppID:"XXXXX",Salt:BaiduTranslate.Salt(5),SecretKey:"XXXXX",From:"auto",To:"en"}
25-
26-
bi.Text = "你好,世界"
27-
fmt.Println(bi.Translate())
28-
/*
29-
bi.To = "wyw" //文言文
30-
fmt.Println(bi.Translate())
31-
bi.To = "jp" //日本语
32-
fmt.Println(bi.Translate())
33-
bi.To = "kor" //韩语
34-
fmt.Println(bi.Translate())
35-
bi.To = "fra" //法语
36-
fmt.Println(bi.Translate())
37-
bi.To = "de" //德语
38-
fmt.Println(bi.Translate())
39-
bi.To = "ru" //俄语
40-
fmt.Println(bi.Translate())
41-
*/
23+
//输入基本信息,Salt长度无要求
24+
// BaiduInfo结构体记录配置项,Salt为数据传送时加盐(库中已有函数实现,可直接调用)
25+
btr := BaiduTranslate.BaiduInfo{AppID: "", Salt: BaiduTranslate.Salt(5), SecretKey: ""}
26+
27+
// 通用翻译
28+
// 传入:(原文, 原文语言, 译文语言)
29+
fmt.Println(btr.NormalTr("Hello world!", "en", "zh"))
30+
fmt.Println(btr.NormalTr("百度翻译", "auto", "de"))
4231
}
32+
4333
```
4434

45-
受支持的翻译语言(源语言语种不确定时可设置为 auto,目标语言语种不可设置为 auto)
35+
## 受支持的翻译语言
36+
37+
**(源语言语种不确定时可设置为 auto,目标语言语种不可设置为 auto)**
4638

4739
| 语言简写 | 名称 |
4840
| :------: | :----------: |

base.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package BaiduTranslate
2+
3+
import (
4+
"log"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
// 百度翻译开放平台信息
10+
type BaiduInfo struct {
11+
AppID string
12+
Salt string
13+
SecretKey string
14+
}
15+
16+
// 自动生盐
17+
// 入口参数为盐的长度
18+
func Salt(l int) string {
19+
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
20+
bytes := []byte(str)
21+
result := []byte{}
22+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
23+
for i := 0; i < l; i++ {
24+
result = append(result, bytes[r.Intn(len(bytes))])
25+
}
26+
return string(result)
27+
}
28+
29+
// 错误检测,如果有错误输出msg
30+
func checkErr(e error, msg string) {
31+
if e != nil {
32+
log.Println(msg)
33+
}
34+
return
35+
}

normal.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package BaiduTranslate
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"encoding/json"
7+
"io/ioutil"
8+
"net/http"
9+
"net/url"
10+
)
11+
12+
func (BaiduInfo *BaiduInfo) NormalTr(Text string, From string, To string) string {
13+
14+
type TransResult struct {
15+
From string `json:"from"`
16+
To string `json:"to"`
17+
Result [1]struct {
18+
Src string `json:"src"`
19+
Dst string `json:"dst"`
20+
} `json:"trans_result"`
21+
ErrorCode string `json:"error_code"`
22+
ErrorMsg string `json:"error_msg"`
23+
}
24+
25+
//合并字符串,计算sign
26+
montage := BaiduInfo.AppID + Text + BaiduInfo.Salt + BaiduInfo.SecretKey
27+
ctx := md5.New()
28+
ctx.Write([]byte(montage))
29+
sign := hex.EncodeToString(ctx.Sum(nil))
30+
31+
// 翻译 传入需要翻译的语句
32+
urlstr := "http://fanyi-api.baidu.com/api/trans/vip/translate?q=" + url.QueryEscape(Text) + "&from=" + From + "&to=" + To + "&appid=" + BaiduInfo.AppID + "&salt=" + BaiduInfo.Salt + "&sign=" + sign
33+
34+
// 发送GET请求
35+
resp, err := http.Get(urlstr)
36+
checkErr(err, "HTTP GET出现错误!")
37+
defer resp.Body.Close()
38+
body, err := ioutil.ReadAll(resp.Body)
39+
40+
var ts TransResult
41+
_ = json.Unmarshal(body, &ts)
42+
if ts.ErrorCode != "" {
43+
errmsg := "错误码:" + ts.ErrorCode + ",错误信息:" + ts.ErrorMsg
44+
return errmsg
45+
} else {
46+
return ts.Result[0].Dst
47+
}
48+
}
49+
50+
/*
51+
52+
// 返回结果
53+
type TransResult struct {
54+
From string `json:"from"`
55+
To string `json:"to"`
56+
Result [1]Result `json:"trans_result"`
57+
ErrorCode string `json:"error_code"`
58+
ErrorMsg string `json:"error_msg"`
59+
60+
Data [1]Data `json:"data"`
61+
}
62+
type Result struct {
63+
Src string `json:"src"`
64+
Dst string `json:"dst"`
65+
}
66+
type Data struct {
67+
Src string `json:"src"`
68+
}
69+
70+
// 生成32位MD5
71+
func Sign(bi *BaiduInfo) string {
72+
text := bi.AppID + bi.Text + bi.Salt + bi.SecretKey
73+
ctx := md5.New()
74+
ctx.Write([]byte(text))
75+
return hex.EncodeToString(ctx.Sum(nil))
76+
}
77+
78+
// 翻译 传入需要翻译的语句
79+
func (bi *BaiduInfo) Translate() string {
80+
url := "http://api.fanyi.baidu.com/api/trans/vip/translate?q=" + bi.Text + "&from=" + bi.From + "&to=" + bi.To + "&appid=" + bi.AppID + "&salt=" + bi.Salt + "&sign=" + Sign(bi)
81+
resp, err := http.Get(url)
82+
if err != nil {
83+
log.Println("网络异常")
84+
}
85+
defer resp.Body.Close()
86+
body, err := ioutil.ReadAll(resp.Body)
87+
var ts TransResult
88+
_ = json.Unmarshal(body, &ts)
89+
if ts.ErrorCode != "" {
90+
return ts.ErrorMsg
91+
} else {
92+
return ts.Result[0].Dst
93+
}
94+
}
95+
96+
func (bi *BaiduInfo) Detect() string {
97+
url := "http://api.fanyi.baidu.com/api/trans/vip/language?q=" + bi.Text + "&appid=" + bi.AppID + "&salt=" + bi.Salt + "&sign=" + Sign(bi)
98+
resp, err := http.Get(url)
99+
if err != nil {
100+
log.Println("网络异常")
101+
}
102+
defer resp.Body.Close()
103+
body, err := ioutil.ReadAll(resp.Body)
104+
var ts TransResult
105+
_ = json.Unmarshal(body, &ts)
106+
if ts.ErrorCode != "" {
107+
return ts.ErrorCode
108+
} else {
109+
return ts.Data[0].Src
110+
}
111+
}
112+
113+
*/

trans.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

trans_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)