-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepl_client.go
62 lines (55 loc) · 1.25 KB
/
deepl_client.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/tidwall/gjson"
)
func RunDeepl(text string, from_lang string, to_lang string) (string, error) {
params := url.Values{}
params.Add("text", text)
params.Add("source_lang", from_lang)
params.Add("target_lang", to_lang)
params.Add("preserve_formatting", "1")
body := strings.NewReader(params.Encode())
req, err := http.NewRequest(
"POST",
DeeplAPIURL,
body,
)
if err != nil {
fmt.Println("Error creating Deepl request")
return "", err
}
req.Header.Set(
"Authorization",
fmt.Sprintf(`DeepL-Auth-Key %s`, os.Getenv("DEEPL_TOKEN")),
)
req.Header.Set(
"Content-Type",
"application/x-www-form-urlencoded",
)
resp, err := http.DefaultClient.Do(req)
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Error executing Deepl request")
fmt.Println("Status", resp.Status)
fmt.Println("Body", string(bodyText))
os.Exit(1)
}
translated_text := gjson.Get(
string(bodyText),
"translations.0.text",
).Str
if text[0:1] == " " {
translated_text = " " + translated_text
}
if text[len(text)-1:] == " " {
translated_text = translated_text + " "
}
return translated_text, nil
}