Skip to content

Commit

Permalink
fix bing translaltor
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Jul 25, 2019
1 parent c32eaf9 commit 83ac7ed
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions bing.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,16 @@ func BingTranslate(ctx context.Context, req Request) *Response {

func (b *bingTranslator) translate(ctx context.Context, req Request) (resp *Response) {
resp = newResp(b)

detectUrl := "https://www.bing.com/tdetect/"
param := url.Values{"text": {req.Text}}
body := strings.NewReader(param.Encode())
_, data, err := sendRequest(ctx, http.MethodPost, detectUrl, body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil
})
from := string(data)

req.ToLang = b.convertLanguage(req.ToLang)
param = url.Values{
"from": {from},
"to": {req.ToLang},
"text": {req.Text},
param := url.Values{
"fromLang": {"auto-detect"},
"to": {req.ToLang},
"text": {req.Text},
}

urlStr := "https://www.bing.com/ttranslate/"
body = strings.NewReader(param.Encode())
_, data, err = sendRequest(ctx, "POST", urlStr, body, func(req *http.Request) error {
urlStr := "https://www.bing.com/ttranslatev3"
body := strings.NewReader(param.Encode())
_, data, err := sendRequest(ctx, "POST", urlStr, body, func(req *http.Request) error {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return nil
})
Expand All @@ -53,7 +43,10 @@ func (b *bingTranslator) translate(ctx context.Context, req Request) (resp *Resp
}

jr := gjson.Parse(string(data))
resp.Result = jr.Get("translationResponse").String()
resp.Result = jr.Get("0.translations.0.text").String()
if resp.Result == "" {
resp.Err = fmt.Errorf(string(data))
}
return
}

Expand Down

0 comments on commit 83ac7ed

Please sign in to comment.