Skip to content

Commit

Permalink
feat: make host of DeepLX configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeXun committed Feb 10, 2024
1 parent ff46861 commit 943def5
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See the example in [server.yaml](example/server.yaml) file.
api_key:
deeplx: DEEPLX_API_KEY # <- Replace with your API Key
host:
deeplx: 127.0.0.1:1188 # <- Replace with your DeepLX server IP address and port
deeplx: 127.0.0.1:1188 # <- Replace with your server IP address and port
```

## ScreenShot
Expand Down
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,18 @@ func configInit() {
uiStyle.Transparent = config.GetBool("transparent")
uiStyle.SetSrcBorderColor(config.GetString("source.border_color")).
SetDstBorderColor(config.GetString("destination.border_color"))
// Import api key if file exists
// Import api key and host if file exists
if err := serverConfig.ReadInConfig(); err == nil {
// api key
for _, name := range []string{"ChatGPT", "DeepL", "DeepLX"} {
if serverConfig.Get(fmt.Sprintf("api_key.%s", name)) != nil {
translators[name].SetAPIKey(serverConfig.GetString(fmt.Sprintf("api_key.%s", name)))
}
}
// host
if serverConfig.Get("host.deeplx") != nil {
translators["DeepLX"].SetHost(serverConfig.GetString("host.deeplx"))
}
}
// Set argument language
if len(*srcLangArg) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions example/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ api_key:
chatgpt: CHATGPT_API_KEY
deepl: DEEPL_API_KEY
deeplx: DEEPLX_API_KEY
host:
deeplx: 127.0.0.1:1188
4 changes: 2 additions & 2 deletions internal/translate/apertium/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("Apertium"),
Expand Down
4 changes: 2 additions & 2 deletions internal/translate/argos/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("Argos"),
Expand Down
4 changes: 2 additions & 2 deletions internal/translate/bing/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
Expand All @@ -40,7 +40,7 @@ type setUpData struct {

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("Bing"),
Expand Down
4 changes: 2 additions & 2 deletions internal/translate/chatgpt/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("ChatGPT"),
Expand Down
13 changes: 0 additions & 13 deletions internal/translate/core/apikey.go

This file was deleted.

22 changes: 22 additions & 0 deletions internal/translate/core/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core

type Server struct {
host string
apiKey string
}

func (s *Server) SetHost(host string) {
s.host = host
}

func (s *Server) GetHost() string {
return s.host
}

func (s *Server) SetAPIKey(key string) {
s.apiKey = key
}

func (s *Server) GetAPIKey() string {
return s.apiKey
}
4 changes: 2 additions & 2 deletions internal/translate/deepl/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("DeepL"),
Expand Down
14 changes: 7 additions & 7 deletions internal/translate/deeplx/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import (
"github.com/eeeXun/gtt/internal/translate/core"
)

const (
textURL = "http://localhost:1188/translate"
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("DeepLX"),
Expand All @@ -38,13 +34,17 @@ func (t *Translator) Translate(message string) (translation *core.Translation, e
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,
textURL,
"http://"+t.GetHost()+"/translate",
bytes.NewBuffer(userData),
)
req.Header.Add("Content-Type", "application/json")
Expand Down
4 changes: 2 additions & 2 deletions internal/translate/google/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("Google"),
Expand Down
4 changes: 2 additions & 2 deletions internal/translate/reverso/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const (
)

type Translator struct {
*core.APIKey
*core.Server
*core.Language
*core.TTSLock
core.EngineName
}

func NewTranslator() *Translator {
return &Translator{
APIKey: new(core.APIKey),
Server: new(core.Server),
Language: new(core.Language),
TTSLock: core.NewTTSLock(),
EngineName: core.NewEngineName("Reverso"),
Expand Down
3 changes: 3 additions & 0 deletions internal/translate/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Translator interface {
// Set API Key
SetAPIKey(key string)

// Set host
SetHost(host string)

// Check if lock is available
LockAvailable() bool

Expand Down

0 comments on commit 943def5

Please sign in to comment.