Skip to content

Commit

Permalink
refactor: move DeepLX to DeepL
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeXun committed Aug 10, 2024
1 parent 0fe050b commit 5bc48c5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 156 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ See the example in [server.yaml](example/server.yaml) file.
```yaml
api_key:
deeplx:
value: DEEPLX_API_KEY # <- Replace with your API Key
value: DEEPLX_API_KEY # <- Replace with your TOKEN
# file: $HOME/secrets/deeplx.txt # <- You can also specify the file where to read API Key
host:
deeplx: 127.0.0.1:1188 # <- Replace with your server IP address and port
Expand Down
59 changes: 56 additions & 3 deletions internal/translate/deepl/translator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package deepl

import (
"bytes"
"encoding/json"
"errors"
"io"
Expand All @@ -22,20 +23,20 @@ type Translator struct {
core.EngineName
}

func NewTranslator() *Translator {
func NewTranslator(name string) *Translator {
return &Translator{
Server: new(core.Server),
Language: new(core.Language),
TTS: core.NewTTS(),
EngineName: core.NewEngineName("DeepL"),
EngineName: core.NewEngineName(name),
}
}

func (t *Translator) GetAllLang() []string {
return lang
}

func (t *Translator) Translate(message string) (translation *core.Translation, err error) {
func (t *Translator) deeplTranslate(message string) (translation *core.Translation, err error) {
translation = new(core.Translation)
var data map[string]interface{}

Expand Down Expand Up @@ -75,6 +76,58 @@ func (t *Translator) Translate(message string) (translation *core.Translation, e
return translation, nil
}

func (t *Translator) deeplxTranslate(message string) (translation *core.Translation, err error) {
translation = new(core.Translation)
var data map[string]interface{}

if len(t.GetHost()) <= 0 {
return nil, errors.New("Please write your host in config file for " + t.GetEngineName())
}

userData, _ := json.Marshal(map[string]interface{}{
"text": message,
"source_lang": langCode[t.GetSrcLang()],
"target_lang": langCode[t.GetDstLang()],
})
req, _ := http.NewRequest(http.MethodPost,
"http://"+t.GetHost()+"/translate",
bytes.NewBuffer(userData),
)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+t.GetAPIKey())
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &data); err != nil {
return nil, err
}

if len(data) <= 0 {
return nil, errors.New("Translation not found")
}
if res.StatusCode != 200 {
return nil, errors.New(data["message"].(string))
}

translation.TEXT = data["data"].(string)

return translation, nil
}

func (t *Translator) Translate(message string) (translation *core.Translation, err error) {
switch t.GetEngineName() {
case "DeepLX":
return t.deeplxTranslate(message)
default:
return t.deeplTranslate(message)
}
}

func (t *Translator) PlayTTS(lang, message string) error {
defer t.ReleaseLock()

Expand Down
67 changes: 0 additions & 67 deletions internal/translate/deeplx/language.go

This file was deleted.

80 changes: 0 additions & 80 deletions internal/translate/deeplx/translator.go

This file was deleted.

7 changes: 2 additions & 5 deletions internal/translate/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/eeeXun/gtt/internal/translate/chatgpt"
"github.com/eeeXun/gtt/internal/translate/core"
"github.com/eeeXun/gtt/internal/translate/deepl"
"github.com/eeeXun/gtt/internal/translate/deeplx"
"github.com/eeeXun/gtt/internal/translate/google"
"github.com/eeeXun/gtt/internal/translate/libre"
"github.com/eeeXun/gtt/internal/translate/reverso"
Expand Down Expand Up @@ -79,10 +78,8 @@ func NewTranslator(name string) Translator {
translator = bing.NewTranslator()
case "ChatGPT":
translator = chatgpt.NewTranslator()
case "DeepL":
translator = deepl.NewTranslator()
case "DeepLX":
translator = deeplx.NewTranslator()
case "DeepL", "DeepLX":
translator = deepl.NewTranslator(name)
case "Google":
translator = google.NewTranslator()
case "Libre":
Expand Down

0 comments on commit 5bc48c5

Please sign in to comment.