mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-18 00:30:40 -07:00
feat: add DeepLX
This commit is contained in:
parent
d83cbabc35
commit
72034636af
20
README.md
20
README.md
@ -8,6 +8,7 @@ Supported Translator:
|
||||
[`Bing`](https://www.bing.com/translator),
|
||||
[`ChatGPT`](https://chat.openai.com/),
|
||||
[`DeepL`](https://deepl.com/translator)(only free API),
|
||||
[`DeepLX`](https://github.com/OwO-Network/DeepLX),
|
||||
[`Google`](https://translate.google.com/)(default),
|
||||
[`Reverso`](https://www.reverso.net/text-translation)
|
||||
|
||||
@ -17,8 +18,8 @@ ChatGPT and DeepL translations require API keys, which can be obtained from
|
||||
[OpenAI API keys](https://platform.openai.com/account/api-keys) and
|
||||
[DeepL API signup](https://www.deepl.com/pro-api) pages, respectively. Note
|
||||
that only the free API is supported for DeepL currently. Once you have your
|
||||
API key add it to `$XDG_CONFIG_HOME/gtt/server.yaml` or
|
||||
`$HOME/.config/gtt/server.yaml`
|
||||
API key add it to `$XDG_CONFIG_HOME/gtt/server.yaml` or `$HOME/.config/gtt/server.yaml`.
|
||||
See the example in [server.yaml](example/server.yaml) file.
|
||||
|
||||
```yaml
|
||||
api_key:
|
||||
@ -26,6 +27,20 @@ api_key:
|
||||
deepl: DEEPL_API_KEY # <- Replace with your API Key
|
||||
```
|
||||
|
||||
## DeepLX
|
||||
|
||||
The DeepLX is self-hosted server. You must provide IP address and port at
|
||||
`$XDG_CONFIG_HOME/gtt/server.yaml` or `$HOME/.config/gtt/server.yaml`.
|
||||
The api key for DeepLX is optional, depending on your setting.
|
||||
See the example in [server.yaml](example/server.yaml) file.
|
||||
|
||||
```yaml
|
||||
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
|
||||
```
|
||||
|
||||
## ScreenShot
|
||||
|
||||

|
||||
@ -194,6 +209,7 @@ See available languages on:
|
||||
- [Bing language-support](https://learn.microsoft.com/en-us/azure/cognitive-services/translator/language-support#translation) for `Bing`
|
||||
- `ChatGPT` is same as `Google`. See [Google Language support](https://cloud.google.com/translate/docs/languages)
|
||||
- [DeepL API docs](https://www.deepl.com/docs-api/translate-text/) for `DeepL`
|
||||
- `DeepLX` is same as `DeepL`. See [DeepL API docs](https://cloud.google.com/translate/docs/languages)
|
||||
- [Google Language support](https://cloud.google.com/translate/docs/languages) for `Google`
|
||||
- [Reverso Translation](https://www.reverso.net/text-translation) for `Reverso`
|
||||
|
||||
|
@ -51,6 +51,8 @@ func configInit() {
|
||||
"destination.language.chatgpt": "English",
|
||||
"source.language.deepl": "English",
|
||||
"destination.language.deepl": "English",
|
||||
"source.language.deeplx": "English",
|
||||
"destination.language.deeplx": "English",
|
||||
"source.language.google": "English",
|
||||
"destination.language.google": "English",
|
||||
"source.language.reverso": "English",
|
||||
@ -156,7 +158,7 @@ func configInit() {
|
||||
SetDstBorderColor(config.GetString("destination.border_color"))
|
||||
// Import api key if file exists
|
||||
if err := serverConfig.ReadInConfig(); err == nil {
|
||||
for _, name := range []string{"ChatGPT", "DeepL"} {
|
||||
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)))
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
# This file should be located at $XDG_CONFIG_HOME/gtt/server.yaml or $HOME/.config/gtt/server.yaml.
|
||||
api_key:
|
||||
chatgpt: CHATGPT_API_KEY
|
||||
deepl: DEEPL_API_KEY
|
||||
deeplx: DEEPLX_API_KEY
|
||||
|
67
internal/translate/deeplx/language.go
Normal file
67
internal/translate/deeplx/language.go
Normal file
@ -0,0 +1,67 @@
|
||||
package deeplx
|
||||
|
||||
// Generated from DeepL
|
||||
var (
|
||||
lang = []string{
|
||||
"Bulgarian",
|
||||
"Chinese",
|
||||
"Czech",
|
||||
"Danish",
|
||||
"Dutch",
|
||||
"English",
|
||||
"Estonian",
|
||||
"Finnish",
|
||||
"French",
|
||||
"German",
|
||||
"Greek",
|
||||
"Hungarian",
|
||||
"Indonesian",
|
||||
"Italian",
|
||||
"Japanese",
|
||||
"Korean",
|
||||
"Latvian",
|
||||
"Lithuanian",
|
||||
"Norwegian",
|
||||
"Polish",
|
||||
"Portuguese",
|
||||
"Romanian",
|
||||
"Russian",
|
||||
"Slovak",
|
||||
"Slovenian",
|
||||
"Spanish",
|
||||
"Swedish",
|
||||
"Turkish",
|
||||
"Ukrainian",
|
||||
}
|
||||
langCode = map[string]string{
|
||||
"Bulgarian": "BG",
|
||||
"Chinese": "ZH",
|
||||
"Czech": "CS",
|
||||
"Danish": "DA",
|
||||
"Dutch": "NL",
|
||||
"English": "EN",
|
||||
"Estonian": "ET",
|
||||
"Finnish": "FI",
|
||||
"French": "FR",
|
||||
"German": "DE",
|
||||
"Greek": "EL",
|
||||
"Hungarian": "HU",
|
||||
"Indonesian": "ID",
|
||||
"Italian": "IT",
|
||||
"Japanese": "JA",
|
||||
"Korean": "KO",
|
||||
"Latvian": "LV",
|
||||
"Lithuanian": "LT",
|
||||
"Norwegian": "NB",
|
||||
"Polish": "PL",
|
||||
"Portuguese": "PT",
|
||||
"Romanian": "RO",
|
||||
"Russian": "RU",
|
||||
"Slovak": "SK",
|
||||
"Slovenian": "SL",
|
||||
"Spanish": "ES",
|
||||
"Swedish": "SV",
|
||||
"Turkish": "TR",
|
||||
"Ukrainian": "UK",
|
||||
}
|
||||
)
|
80
internal/translate/deeplx/translator.go
Normal file
80
internal/translate/deeplx/translator.go
Normal file
@ -0,0 +1,80 @@
|
||||
package deeplx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/eeeXun/gtt/internal/translate/core"
|
||||
)
|
||||
|
||||
const (
|
||||
textURL = "http://localhost:1188/translate"
|
||||
)
|
||||
|
||||
type Translator struct {
|
||||
*core.APIKey
|
||||
*core.Language
|
||||
*core.TTSLock
|
||||
core.EngineName
|
||||
}
|
||||
|
||||
func NewTranslator() *Translator {
|
||||
return &Translator{
|
||||
APIKey: new(core.APIKey),
|
||||
Language: new(core.Language),
|
||||
TTSLock: core.NewTTSLock(),
|
||||
EngineName: core.NewEngineName("DeepLX"),
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Translator) GetAllLang() []string {
|
||||
return lang
|
||||
}
|
||||
|
||||
func (t *Translator) Translate(message string) (translation *core.Translation, err error) {
|
||||
translation = new(core.Translation)
|
||||
var data map[string]interface{}
|
||||
|
||||
userData, _ := json.Marshal(map[string]interface{}{
|
||||
"text": message,
|
||||
"source_lang": langCode[t.GetSrcLang()],
|
||||
"target_lang": langCode[t.GetDstLang()],
|
||||
})
|
||||
req, _ := http.NewRequest(http.MethodPost,
|
||||
textURL,
|
||||
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) PlayTTS(lang, message string) error {
|
||||
defer t.ReleaseLock()
|
||||
|
||||
return errors.New(t.GetEngineName() + " does not support text to speech")
|
||||
}
|
@ -7,6 +7,7 @@ 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/reverso"
|
||||
)
|
||||
@ -18,6 +19,7 @@ var (
|
||||
"Bing",
|
||||
"ChatGPT",
|
||||
"DeepL",
|
||||
"DeepLX",
|
||||
"Google",
|
||||
"Reverso",
|
||||
}
|
||||
@ -78,6 +80,8 @@ func NewTranslator(name string) Translator {
|
||||
translator = chatgpt.NewTranslator()
|
||||
case "DeepL":
|
||||
translator = deepl.NewTranslator()
|
||||
case "DeepLX":
|
||||
translator = deeplx.NewTranslator()
|
||||
case "Google":
|
||||
translator = google.NewTranslator()
|
||||
case "Reverso":
|
||||
|
Loading…
x
Reference in New Issue
Block a user