add support for disablin engines

This commit is contained in:
Arya 2023-08-30 16:05:16 +05:30
parent 0e2be0cd32
commit fc6f689fb6
Signed by: arya
GPG Key ID: 842D12BDA50DF120
5 changed files with 62 additions and 37 deletions

View File

@ -29,6 +29,23 @@ Mozhi makes use of swagger (with the swagger fiber middleware) to manage the doc
You can find it in /api/swagger of any instance. You can find it in /api/swagger of any instance.
## Configuration
Features of Mozhi can be customized and toggled on/off using Environment Variables.
- `MOZHI_PORT`: Port the webserver listens on (if hosting API)
- `MOZHI_USER_AGENT`: Change user agent used to make HTTP requests
- `MOZHI_LIBRETRANSLATE_URL`: URL of Libretranslate instance (Example: `MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt`)
These envvars turn off/on engines. By default all of them are enabled.
- `MOZHI_GOOGLE_ENABLED`
- `MOZHI_REVERSO_ENABLED`
- `MOZHI_DEEPL_ENABLED`
- `MOZHI_LIBRETRANSLATE_ENABLED`
- `MOZHI_YANDEX_ENABLED`
- `MOZHI_WATSON_ENABLED`
- `MOZHI_MYMEMORY_ENABLED`
- `MOZHI_DUCKDUCKGO_ENABLED`
## Features ## Features
- An all mode where the responses of all supported engines will be shown. - An all mode where the responses of all supported engines will be shown.
- Autodetect which will show the language that was detected - Autodetect which will show the language that was detected

View File

@ -2,7 +2,6 @@
- Create a web interface - Create a web interface
- Interactive/Step-by-step CLI - Interactive/Step-by-step CLI
- CI/CD - CI/CD
- Support for disabling engines
- Ability for user to choose engines they want to use - Ability for user to choose engines they want to use
- Proper Error handling for requests.go - Proper Error handling for requests.go
- Tell which language Detect Language chose -- Only support for deepl and google is pending - Tell which language Detect Language chose -- Only support for deepl and google is pending

View File

@ -23,20 +23,21 @@ var langlistCmd = &cobra.Command{
list, err := utils.LangList(engineused, listtype) list, err := utils.LangList(engineused, listtype)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} } else {
idxs, err := fuzzyfinder.FindMulti( idxs, err := fuzzyfinder.FindMulti(
list, list,
func(i int) string { func(i int) string {
return list[i].Name return list[i].Name
}) })
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
for _, idx := range idxs { for _, idx := range idxs {
if raw == true { if raw == true {
fmt.Println(list[idx].Id) fmt.Println(list[idx].Id)
} else { } else {
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")") fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")
}
} }
} }
}, },

View File

@ -41,6 +41,7 @@ var translateCmd = &cobra.Command{
fmt.Println("Source Language: " + data[i].SourceLang) fmt.Println("Source Language: " + data[i].SourceLang)
fmt.Println("Target Language: " + data[i].TargetLang) fmt.Println("Target Language: " + data[i].TargetLang)
} }
fmt.Println("-----------------------------------")
} }
} else { } else {
data, err := utils.Translate(engine, dest, source, query) data, err := utils.Translate(engine, dest, source, query)

View File

@ -17,32 +17,39 @@ type LangOut struct {
TargetLang string `json:"target_language"` TargetLang string `json:"target_language"`
} }
func EnvTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
}
func LangList(engine string, listType string) ([]List, error) { func LangList(engine string, listType string) ([]List, error) {
var data []List var data []List
if listType != "sl" && listType != "tl" { if listType != "sl" && listType != "tl" {
return []List{}, errors.New("list type invalid: either give tl for target languages or sl for source languages.") return []List{}, errors.New("list type invalid: either give tl for target languages or sl for source languages.")
} }
if engine == "google" { if engine == "google" && EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") {
data = LangListGoogle(listType) data = LangListGoogle(listType)
} else if engine == "libre" { } else if engine == "libre" && EnvTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") {
if os.Getenv("MOZHI_LIBRETRANSLATE_URL") == "" { if EnvTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
return []List{}, errors.New("Please set MOZHI_LIBRETRANSLATE_URL if you want to use libretranslate. Example: MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt") return []List{}, errors.New("Please set MOZHI_LIBRETRANSLATE_URL if you want to use libretranslate. Example: MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt")
} }
data = LangListLibreTranslate(listType) data = LangListLibreTranslate(listType)
} else if engine == "reverso" { } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") {
data = LangListReverso(listType) data = LangListReverso(listType)
} else if engine == "deepl" { } else if engine == "deepl" && EnvTrueNoExist("MOZHI_DEEPL_ENABLED") {
data = LangListDeepl(listType) data = LangListDeepl(listType)
} else if engine == "watson" { } else if engine == "watson" && EnvTrueNoExist("MOZHI_WATSON_ENABLED") {
data = LangListWatson(listType) data = LangListWatson(listType)
} else if engine == "yandex" { } else if engine == "yandex" && EnvTrueNoExist("MOZHI_YANDEX_ENABLED") {
data = LangListYandex(listType) data = LangListYandex(listType)
} else if engine == "mymemory" { } else if engine == "mymemory" && EnvTrueNoExist("MOZHI_MYMEMORY_ENABLED") {
data = LangListMyMemory(listType) data = LangListMyMemory(listType)
} else if engine == "duckduckgo" { } else if engine == "duckduckgo" && EnvTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") {
data = LangListDuckDuckGo(listType) data = LangListDuckDuckGo(listType)
} else { } else {
return []List{}, errors.New("Engine does not exist.") return []List{}, errors.New("Engine does not exist or has been disabled.")
} }
return data, nil return data, nil
} }
@ -51,27 +58,27 @@ func LangList(engine string, listType string) ([]List, error) {
func Translate(engine string, to string, from string, text string) (LangOut, error) { func Translate(engine string, to string, from string, text string) (LangOut, error) {
var err error var err error
var data LangOut var data LangOut
if engine == "google" { if engine == "google" && EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") {
data, err = TranslateGoogle(to, from, text) data, err = TranslateGoogle(to, from, text)
} else if engine == "libre" { } else if engine == "libre" && EnvTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") {
if os.Getenv("MOZHI_LIBRETRANSLATE_URL") == "" { if os.Getenv("MOZHI_LIBRETRANSLATE_URL") == "" {
return LangOut{}, errors.New("Please set MOZHI_LIBRETRANSLATE_URL if you want to use libretranslate. Example: MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt") return LangOut{}, errors.New("Please set MOZHI_LIBRETRANSLATE_URL if you want to use libretranslate. Example: MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt")
} }
data, err = TranslateLibreTranslate(to, from, text) data, err = TranslateLibreTranslate(to, from, text)
} else if engine == "reverso" { } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") {
data, err = TranslateReverso(to, from, text) data, err = TranslateReverso(to, from, text)
} else if engine == "deepl" { } else if engine == "deepl" && EnvTrueNoExist("MOZHI_DEEPL_ENABLED") {
data, err = TranslateDeepl(to, from, text) data, err = TranslateDeepl(to, from, text)
} else if engine == "watson" { } else if engine == "watson" && EnvTrueNoExist("MOZHI_WATSON_ENABLED") {
data, err = TranslateWatson(to, from, text) data, err = TranslateWatson(to, from, text)
} else if engine == "yandex" { } else if engine == "yandex" && EnvTrueNoExist("MOZHI_YANDEX_ENABLED") {
data, err = TranslateYandex(to, from, text) data, err = TranslateYandex(to, from, text)
} else if engine == "mymemory" { } else if engine == "mymemory" && EnvTrueNoExist("MOZHI_MYMEMORY_ENABLED") {
data, err = TranslateMyMemory(to, from, text) data, err = TranslateMyMemory(to, from, text)
} else if engine == "duckduckgo" { } else if engine == "duckduckgo" && EnvTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") {
data, err = TranslateDuckDuckGo(to, from, text) data, err = TranslateDuckDuckGo(to, from, text)
} else { } else {
return LangOut{}, errors.New("Engine does not exist.") return LangOut{}, errors.New("Engine does not exist or has been disabled.")
} }
if err != nil { if err != nil {
return LangOut{}, err return LangOut{}, err
@ -82,12 +89,12 @@ func Translate(engine string, to string, from string, text string) (LangOut, err
func TTS(engine string, lang string, text string) ([]byte, error) { func TTS(engine string, lang string, text string) ([]byte, error) {
var err error var err error
var data []byte var data []byte
if engine == "google" { if engine == "google" && EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") {
data, err = TTSGoogle(lang, text) data, err = TTSGoogle(lang, text)
} else if engine == "reverso" { } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") {
data, err = TTSReverso(lang, text) data, err = TTSReverso(lang, text)
} else { } else {
return []byte(""), errors.New("Engine does not exist and/or doesn't support TTS.") return []byte(""), errors.New("Engine does not exist and/or doesn't support TTS and/or has been disabled.")
} }
if err != nil { if err != nil {
return []byte(""), err return []byte(""), err