/
bing.go
87 lines (73 loc) · 1.87 KB
/
bing.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package upstream
import (
"bytes"
"encoding/xml"
"github.com/pkg/errors"
"golang.org/x/text/language"
"log"
"net/http"
"net/url"
)
const (
azureAPIBase = "https://api.microsofttranslator.com/v2/Http.svc/Translate"
)
var azureBaseURL *url.URL
// Azure represents a translation service calling the Azure Cognitive Services Machine Translation Service.
type Azure struct {
// ServiceKey is the key from the Azure dashboard
ServiceKey string
}
type bingResult struct {
// Translated is the translation content as returned from Azure
Translated string `xml:",chardata"`
}
func init() {
var err error
azureBaseURL, err = url.Parse(azureAPIBase)
if err != nil {
panic(err)
}
}
// Translate call Microsoft Cognitive Services to translate the given string
func (b Azure) Translate(givenPhrase string, givenLang, targetLang language.Tag, out *chan Result) {
requestURL := *azureBaseURL
requestURL.RawQuery = "from=" + url.QueryEscape(givenLang.String()) + "&to=" + url.QueryEscape(targetLang.String()) + "&text=" + url.QueryEscape(givenPhrase)
request, err := http.NewRequest(http.MethodGet, requestURL.String(), nil)
request.Header.Set("Ocp-Apim-Subscription-Key", b.ServiceKey)
if err != nil {
*out <- Result{
Error: err,
}
return
}
response, err := http.DefaultClient.Do(request)
if err != nil {
*out <- Result{
Error: err,
}
return
}
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
content := buf.String()
if response.StatusCode != http.StatusOK {
log.Printf("Azure API returned error: %v", content)
*out <- Result{
Error: errors.New(response.Status),
}
}
result := &bingResult{}
err = xml.Unmarshal(buf.Bytes(), result)
if err != nil {
*out <- Result{
Error: err,
}
return
}
*out <- Result{
GivenLang: givenLang,
GivenPhrase: givenPhrase,
TargetLang: targetLang,
TranslatedPhrase: result.Translated,
}
}