Skip to content

Commit

Permalink
feat: Add translate by deeplx
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Nov 23, 2024
1 parent b5962cc commit 6eaa023
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
29 changes: 25 additions & 4 deletions deeplx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package deeplx
import (
"bufio"
"encoding/json"
"github.com/OwO-Network/DeepLX/translate"
"github.com/abadojack/whatlanggo"
"github.com/avast/retry-go"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -105,7 +106,7 @@ func Translate(text, sourceLang, targetLang string) *Response {
jsonBody, _ := json.Marshal(req)

var body []byte
_ = retry.Do(
err := retry.Do(
func() error {
client := &http.Client{
Timeout: 3 * time.Second,
Expand All @@ -128,9 +129,29 @@ func Translate(text, sourceLang, targetLang string) *Response {
retry.LastErrorOnly(true),
)

if err == nil {
return &Response{
Code: gjson.Get(string(body), "code").Int(),
Data: gjson.Get(string(body), "data").String(),
Msg: gjson.Get(string(body), "message").String(),
}
}

return TranslateByDeeplx(text, sourceLang, targetLang)
}

func TranslateByDeeplx(text, sourceLang, targetLang string) *Response {
result, err := translate.TranslateByDeepLX(sourceLang, targetLang, text, "", "")
if err != nil {
return &Response{
Code: 500,
Msg: err.Error(),
}
}

return &Response{
Code: gjson.Get(string(body), "code").Int(),
Data: gjson.Get(string(body), "data").String(),
Msg: gjson.Get(string(body), "message").String(),
Code: int64(result.Code),
Data: result.Data,
Msg: result.Message,
}
}
6 changes: 6 additions & 0 deletions deeplx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ func TestTranslateWithGo(t *testing.T) {
end := time.Now().Sub(start).Seconds()
t.Log(fmt.Sprintf("time: %.2f", end))
}

func TestTranslateByDeeplx(t *testing.T) {
response := TranslateByDeeplx("Hello", "En", "zh")
assert.Equal(t, int64(200), response.Code)
t.Log(response)
}
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
module github.com/xiaoxuan6/deeplx

go 1.22.5
go 1.23.2

toolchain go1.23.3

require (
github.com/OwO-Network/DeepLX v0.9.8
github.com/abadojack/whatlanggo v1.0.1
github.com/avast/retry-go v2.7.0+incompatible
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
22 changes: 21 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
github.com/OwO-Network/DeepLX v0.9.8 h1:dfCX0czfbH+om0rNXOl9yFCGvLbiC4yXSen29GLHB4k=
github.com/OwO-Network/DeepLX v0.9.8/go.mod h1:LDlz2LoZrJIfxiZjZJKAZP3uF1DoHUzRcakwjM/08kQ=
github.com/abadojack/whatlanggo v1.0.1 h1:19N6YogDnf71CTHm3Mp2qhYfkRdyvbgwWdd2EPxJRG4=
github.com/abadojack/whatlanggo v1.0.1/go.mod h1:66WiQbSbJBIlOZMsvbKe5m6pzQovxCH9B/K8tQB2uoc=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/avast/retry-go v2.7.0+incompatible h1:XaGnzl7gESAideSjr+I8Hki/JBi+Yb9baHlMRPeSC84=
github.com/avast/retry-go v2.7.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
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/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94=
Expand All @@ -14,7 +31,10 @@ github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 6eaa023

Please sign in to comment.