-
Notifications
You must be signed in to change notification settings - Fork 7
/
deepl.go
426 lines (362 loc) · 11.6 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package deepl
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
httpi "github.com/bounoable/deepl/http"
)
const (
// V2 is the base url for v2 of the deepl API.
V2 = "https://api.deepl.com/v2"
)
// A Client is a deepl client.
type Client struct {
client httpi.Client
authKey string
baseURL string
translateURL string
glossaryURL string
}
// A ClientOption configures a Client.
type ClientOption func(*Client)
// A TranslateOption configures a translation request.
type TranslateOption func(url.Values)
// Error is a DeepL error.
type Error struct {
// The HTTP error code, returned by the DeepL API.
Code int
Body []byte
}
// BaseURL returns a ClientOption that sets the base url for requests.
func BaseURL(url string) ClientOption {
return func(c *Client) {
c.baseURL = url
c.translateURL = fmt.Sprintf("%s/translate", c.baseURL)
c.glossaryURL = fmt.Sprintf("%s/glossaries", c.baseURL)
}
}
// HTTPClient returns a ClientOption that specifies the http.Client that's used
// when making requests.
func HTTPClient(client httpi.Client) ClientOption {
return func(c *Client) {
c.client = client
}
}
// SourceLang returns a ClientOption that specifies the source language of the
// input text. If SourceLang is not used, DeepL automatically figures out the
// source language.
func SourceLang(lang Language) TranslateOption {
return func(vals url.Values) {
vals.Set("source_lang", string(lang))
}
}
// SplitSentences returns a TranslateOption that sets the `split_sentences`
// DeepL option.
func SplitSentences(split SplitSentence) TranslateOption {
return func(vals url.Values) {
vals.Set("split_sentences", split.Value())
}
}
// PreserveFormatting returns a TranslateOption that sets the
// `preserve_formatting` DeepL option.
func PreserveFormatting(preserve bool) TranslateOption {
return func(vals url.Values) {
vals.Set("preserve_formatting", boolString(preserve))
}
}
// Formality returns a TranslateOption that sets the `formality` DeepL option.
func Formality(formal Formal) TranslateOption {
return func(vals url.Values) {
vals.Set("formality", formal.Value())
}
}
// TagHandling returns a TranslateOption that sets the `tag_handling` DeepL
// option.
func TagHandling(handling TagHandlingStrategy) TranslateOption {
return func(vals url.Values) {
vals.Set("tag_handling", handling.Value())
}
}
// IgnoreTags returns a TranslateOption that sets the `ignore_tags` DeepL
// option.
func IgnoreTags(tags ...string) TranslateOption {
return func(vals url.Values) {
vals.Set("ignore_tags", strings.Join(tags, ","))
}
}
// GlossaryID returns a TranslateOption that sets the `glossary_id` DeepL
// option.
func GlossaryID(glossaryID string) TranslateOption {
return func(vals url.Values) {
vals.Set("glossary_id", glossaryID)
}
}
// Context returns a TranslateOption that sets the `context` DeepL
// option.
func Context(context string) TranslateOption {
return func(vals url.Values) {
vals.Set("context", context)
}
}
// New returns a Client that uses authKey as the DeepL authentication key.
func New(authKey string, opts ...ClientOption) *Client {
c := Client{
authKey: authKey,
client: http.DefaultClient,
}
// default base url
BaseURL(V2)(&c)
for _, opt := range opts {
opt(&c)
}
return &c
}
// HTTPClient returns the underlying http.Client.
func (c *Client) HTTPClient() httpi.Client {
return c.client
}
// BaseURL returns the configured base url for requests.
func (c *Client) BaseURL() string {
return c.baseURL
}
// AuthKey returns the DeepL authentication key.
func (c *Client) AuthKey() string {
return c.authKey
}
// Translate translates the provided text into the specified Language and
// returns the translated text and the detected source Language of the input
// text.
//
// When DeepL responds with an error, Translate returns an Error that contains
// the DeepL error code and message. Use errors.As to unwrap the returned error
// into an Error:
//
// trans, sourceLang, err := c.Translate(context.TODO(), "Hello.", deepl.Japanese)
// var deeplError deepl.Error
// if errors.As(err, &deeplError) {
// log.Println(fmt.Sprintf("DeepL error code %d: %s", deeplError.Code, deeplError))
// }
func (c *Client) Translate(ctx context.Context, text string, targetLang Language, opts ...TranslateOption) (string, Language, error) {
translations, err := c.TranslateMany(ctx, []string{text}, targetLang, opts...)
if err != nil {
return "", "", fmt.Errorf("translate many: %w", err)
}
if len(translations) == 0 {
return "", "", errors.New("deepl responded with no translations")
}
return translations[0].Text, Language(translations[0].DetectedSourceLanguage), nil
}
// TranslateMany translates the provided texts into the specified Language and
// returns a Translation for every input text. The order of the translated texts
// is guaranteed to be the same as the order of the input texts.
//
// When DeepL responds with an error, TranslateMany returns an Error that
// contains the DeepL error code and message. Use errors.As to unwrap the
// returned error into an Error:
//
// translations, err := c.TranslateMany(
// context.TODO(),
// []string{"Hello", "World"},
// deepl.Japanese,
// )
// var deeplError deepl.Error
// if errors.As(err, &deeplError) {
// log.Println(fmt.Sprintf("DeepL error code %d: %s", deeplError.Code, deeplError))
// }
func (c *Client) TranslateMany(ctx context.Context, texts []string, targetLang Language, opts ...TranslateOption) ([]Translation, error) {
vals := make(url.Values)
vals.Set("target_lang", string(targetLang))
for _, text := range texts {
vals.Add("text", text)
}
for _, opt := range opts {
opt(vals)
}
req, err := http.NewRequestWithContext(ctx, "POST", c.translateURL, strings.NewReader(vals.Encode()))
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, Error{Code: resp.StatusCode}
}
var response translateResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("decode deepl response: %w", err)
}
return response.Translations, nil
}
func errorFromResp(r *http.Response) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("read response body: %w", err)
}
return Error{
Code: r.StatusCode,
Body: b,
}
}
// CreateGlossary as per
// https://www.deepl.com/docs-api/managing-glossaries/creating-a-glossary/
func (c *Client) CreateGlossary(ctx context.Context, name string, sourceLang, targetLang Language, entries []GlossaryEntry) (*Glossary, error) {
vals := make(url.Values)
vals.Set("name", name)
vals.Set("source_lang", string(sourceLang))
vals.Set("target_lang", string(targetLang))
vals.Set("entries_format", "tsv")
entriesTSV := make([]string, 0, len(entries))
for _, entry := range entries {
entriesTSV = append(entriesTSV, entry.Source+"\t"+entry.Target)
}
vals.Set("entries", strings.Join(entriesTSV, "\n"))
req, err := http.NewRequestWithContext(ctx, "POST", c.glossaryURL, strings.NewReader(vals.Encode()))
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return nil, errorFromResp(resp)
}
var response Glossary
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("decode deepl response: %w", err)
}
return &response, nil
}
// ListGlossaries as per
// https://www.deepl.com/docs-api/managing-glossaries/listing-glossaries/
func (c *Client) ListGlossaries(ctx context.Context) ([]Glossary, error) {
req, err := http.NewRequestWithContext(ctx, "GET", c.glossaryURL, nil)
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResp(resp)
}
var response struct {
Glossaries []Glossary `json:"glossaries"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("decode deepl response: %w", err)
}
return response.Glossaries, nil
}
// ListGlossary as per
// https://www.deepl.com/docs-api/managing-glossaries/listing-glossary-information/
func (c *Client) ListGlossary(ctx context.Context, glossaryID string) (*Glossary, error) {
req, err := http.NewRequestWithContext(ctx, "GET", c.glossaryURL+"/"+glossaryID, nil)
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResp(resp)
}
var response Glossary
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("decode deepl response: %w", err)
}
return &response, nil
}
// ListGlossaryEntries as per
// https://www.deepl.com/docs-api/managing-glossaries/listing-entries-of-a-glossary/
func (c *Client) ListGlossaryEntries(ctx context.Context, glossaryID string) ([]GlossaryEntry, error) {
req, err := http.NewRequestWithContext(ctx, "GET", c.glossaryURL+"/"+glossaryID+"/entries", nil)
if err != nil {
return nil, fmt.Errorf("build request: %w", err)
}
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
req.Header.Add("Accept", "text/tab-separated-values")
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResp(resp)
}
var entries []GlossaryEntry
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, "\t")
if len(parts) != 2 {
return nil, fmt.Errorf("expected 2 tab-separated values, got %q", line)
}
entries = append(entries, GlossaryEntry{
Source: parts[0],
Target: parts[1],
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
return entries, nil
}
// DeleteGlossary as per
// https://www.deepl.com/docs-api/managing-glossaries/deleing-a-glossary/
func (c *Client) DeleteGlossary(ctx context.Context, glossaryID string) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", c.glossaryURL+"/"+glossaryID, nil)
if err != nil {
return fmt.Errorf("build request: %w", err)
}
req.Header.Add("Authorization", "DeepL-Auth-Key "+c.authKey)
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("do request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return errorFromResp(resp)
}
return nil
}
func (err Error) Error() string {
switch err.Code {
case 456:
return "Quota exceeded. The character limit has been reached."
default:
if len(err.Body) > 0 {
return fmt.Sprintf("unexpected HTTP status %s (%s)",
http.StatusText(err.Code),
strings.TrimSpace(string(err.Body)))
}
return fmt.Sprintf("unexpected HTTP status %s",
http.StatusText(err.Code))
}
}
func boolString(b bool) string {
if b {
return "1"
}
return "0"
}