forked from bwmarrin/disgord
/
deepl.go
64 lines (55 loc) · 1.25 KB
/
deepl.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
package tl
import (
"context"
"log"
"os"
"github.com/DaikiYamakawa/deepl-go"
)
var DEEPL_API_KEY string
var DeepLClient *deepl.Client
const (
// LangDE German
LangDE = "DE"
// LangEN English (American)
LangEN = "EN-US"
// LangENGB English (British)
LangENGB = "EN-GB"
// LangES Spanish
LangES = "ES"
// LangFR French
LangFR = "FR"
// LangIT Italian
LangIT = "IT"
// LangJA Japanese
LangJA = "JA"
// LangNL Dutch
LangNL = "NL"
// LangPL Polish
LangPL = "PL"
// LangPTPT Portuguese (European)
LangPTPT = "PT-PT"
// LangPTBR Potuguese (Brazillian)
LangPTBR = "PT-BR"
// LangRU Russian
LangRU = "RU"
// LangZH Chinese
LangZH = "ZH"
// LangAuto Detect
LangAuto = ""
)
func InitDeepL() {
DEEPL_API_KEY = os.Getenv("DEEPL_API_KEY")
client, err := deepl.New("https://api.deepl.com", nil)
if err != nil {
log.Printf("Failed to initialize DeepL client: %s", err)
}
DeepLClient = client
}
func DeepLTranslate(text string, language string) (string, string, error) {
resp, err := DeepLClient.TranslateSentence(context.Background(), text, LangAuto, language)
if err != nil {
log.Printf("Failed to translate text '%s', %s", text, err)
return "", "", err
}
return resp.Translations[0].Text, resp.Translations[0].DetectedSourceLanguage, nil
}