-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
402 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= | ||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= | ||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4= | ||
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= | ||
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= | ||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package deepl | ||
|
||
import ( | ||
"encoding/csv" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
type GlossaryEntry struct { | ||
Source string | ||
Target string | ||
} | ||
|
||
type GlossaryInfo struct { | ||
GlossaryId string `json:"glossary_id"` | ||
Name string `json:"name"` | ||
Ready bool `json:"ready"` | ||
SourceLang string `json:"source_lang"` | ||
TargetLang string `json:"target_lang"` | ||
CreationTime string `json:"creation_time"` | ||
EntryCount int `json:"entry_count"` | ||
} | ||
|
||
func (t *Translator) CreateGlossary(name string, sourceLang string, targetLang string, entries []GlossaryEntry) (*GlossaryInfo, error) { | ||
vals := make(url.Values) | ||
|
||
vals.Set("name", name) | ||
vals.Set("source_lang", sourceLang) | ||
vals.Set("target_lang", targetLang) | ||
vals.Set("entries_format", "tsv") | ||
|
||
var entriesTSV = make([]string, 0, len(entries)) | ||
for _, entry := range entries { | ||
entriesTSV = append(entriesTSV, fmt.Sprintf("%s\t%s", entry.Source, entry.Target)) | ||
} | ||
vals.Set("entries", strings.Join(entriesTSV, "\n")) | ||
|
||
res, err := t.callAPI("POST", "glossaries", vals, nil) | ||
if err != nil { | ||
return nil, err | ||
} else if res.StatusCode != http.StatusCreated { | ||
return nil, HTTPError{StatusCode: res.StatusCode} | ||
} | ||
defer res.Body.Close() | ||
|
||
var glossary GlossaryInfo | ||
if err := json.NewDecoder(res.Body).Decode(&glossary); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &glossary, nil | ||
} | ||
|
||
func (t *Translator) ListGlossaries() ([]GlossaryInfo, error) { | ||
res, err := t.callAPI("GET", "glossaries", nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} else if res.StatusCode != http.StatusOK { | ||
return nil, HTTPError{StatusCode: res.StatusCode} | ||
} | ||
defer res.Body.Close() | ||
|
||
var response struct { | ||
Glossaries []GlossaryInfo `json:"glossaries"` | ||
} | ||
if err := json.NewDecoder(res.Body).Decode(&response); err != nil { | ||
return nil, err | ||
} | ||
|
||
return response.Glossaries, nil | ||
} | ||
|
||
func (t *Translator) GetGlossary(glossaryId string) (*GlossaryInfo, error) { | ||
endpoint := fmt.Sprintf("glossaries/%s", glossaryId) | ||
|
||
res, err := t.callAPI("GET", endpoint, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} else if res.StatusCode != http.StatusOK { | ||
return nil, HTTPError{StatusCode: res.StatusCode} | ||
} | ||
defer res.Body.Close() | ||
|
||
var glossary GlossaryInfo | ||
if err := json.NewDecoder(res.Body).Decode(&glossary); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &glossary, nil | ||
} | ||
|
||
func (t *Translator) DeleteGlossary(glossaryId string) error { | ||
endpoint := fmt.Sprintf("glossaries/%s", glossaryId) | ||
|
||
res, err := t.callAPI("DELETE", endpoint, nil, nil) | ||
if err != nil { | ||
return err | ||
} else if res.StatusCode != http.StatusNoContent { | ||
return HTTPError{StatusCode: res.StatusCode} | ||
} | ||
defer res.Body.Close() | ||
|
||
return nil | ||
} | ||
|
||
func (t *Translator) GetGlossaryEntries(glossaryId string) ([]GlossaryEntry, error) { | ||
endpoint := fmt.Sprintf("glossaries/%s/entries", glossaryId) | ||
|
||
headers := http.Header{} | ||
headers.Set("Accept", "text/tab-separated-values") | ||
|
||
res, err := t.callAPI("GET", endpoint, nil, headers) | ||
if err != nil { | ||
return nil, err | ||
} else if res.StatusCode != http.StatusOK { | ||
return nil, HTTPError{StatusCode: res.StatusCode} | ||
} | ||
defer res.Body.Close() | ||
|
||
r := csv.NewReader(res.Body) | ||
r.Comma = '\t' | ||
records, err := r.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entries := make([]GlossaryEntry, 0, len(records)) | ||
for _, rec := range records { | ||
entries = append(entries, GlossaryEntry{Source: rec[0], Target: rec[1]}) | ||
} | ||
|
||
return entries, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.