diff --git a/README.md b/README.md index 5938eb7..47d103f 100644 --- a/README.md +++ b/README.md @@ -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. +## 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 - An all mode where the responses of all supported engines will be shown. - Autodetect which will show the language that was detected diff --git a/TODO.md b/TODO.md index 8d87b1e..d308d8e 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,6 @@ - Create a web interface - Interactive/Step-by-step CLI - CI/CD -- Support for disabling engines - Ability for user to choose engines they want to use - Proper Error handling for requests.go - Tell which language Detect Language chose -- Only support for deepl and google is pending diff --git a/cmd/langlist.go b/cmd/langlist.go index ded834e..bd1224d 100644 --- a/cmd/langlist.go +++ b/cmd/langlist.go @@ -23,20 +23,21 @@ var langlistCmd = &cobra.Command{ list, err := utils.LangList(engineused, listtype) if err != nil { fmt.Println(err) - } - idxs, err := fuzzyfinder.FindMulti( - list, - func(i int) string { - return list[i].Name - }) - if err != nil { - fmt.Println(err) - } - for _, idx := range idxs { - if raw == true { - fmt.Println(list[idx].Id) - } else { - fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")") + } else { + idxs, err := fuzzyfinder.FindMulti( + list, + func(i int) string { + return list[i].Name + }) + if err != nil { + fmt.Println(err) + } + for _, idx := range idxs { + if raw == true { + fmt.Println(list[idx].Id) + } else { + fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")") + } } } }, diff --git a/cmd/translate.go b/cmd/translate.go index 57eb3db..32c5e54 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -41,6 +41,7 @@ var translateCmd = &cobra.Command{ fmt.Println("Source Language: " + data[i].SourceLang) fmt.Println("Target Language: " + data[i].TargetLang) } + fmt.Println("-----------------------------------") } } else { data, err := utils.Translate(engine, dest, source, query) diff --git a/utils/main.go b/utils/main.go index 4955e4f..2e861b5 100644 --- a/utils/main.go +++ b/utils/main.go @@ -17,32 +17,39 @@ type LangOut struct { 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) { var data []List if listType != "sl" && listType != "tl" { 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) - } else if engine == "libre" { - if os.Getenv("MOZHI_LIBRETRANSLATE_URL") == "" { + } else if engine == "libre" && EnvTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") { + 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") } data = LangListLibreTranslate(listType) - } else if engine == "reverso" { + } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") { data = LangListReverso(listType) - } else if engine == "deepl" { + } else if engine == "deepl" && EnvTrueNoExist("MOZHI_DEEPL_ENABLED") { data = LangListDeepl(listType) - } else if engine == "watson" { + } else if engine == "watson" && EnvTrueNoExist("MOZHI_WATSON_ENABLED") { data = LangListWatson(listType) - } else if engine == "yandex" { + } else if engine == "yandex" && EnvTrueNoExist("MOZHI_YANDEX_ENABLED") { data = LangListYandex(listType) - } else if engine == "mymemory" { + } else if engine == "mymemory" && EnvTrueNoExist("MOZHI_MYMEMORY_ENABLED") { data = LangListMyMemory(listType) - } else if engine == "duckduckgo" { + } else if engine == "duckduckgo" && EnvTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") { data = LangListDuckDuckGo(listType) } else { - return []List{}, errors.New("Engine does not exist.") + return []List{}, errors.New("Engine does not exist or has been disabled.") } 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) { var err error var data LangOut - if engine == "google" { + if engine == "google" && EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") { data, err = TranslateGoogle(to, from, text) - } else if engine == "libre" { + } else if engine == "libre" && EnvTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") { 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") } data, err = TranslateLibreTranslate(to, from, text) - } else if engine == "reverso" { + } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") { data, err = TranslateReverso(to, from, text) - } else if engine == "deepl" { + } else if engine == "deepl" && EnvTrueNoExist("MOZHI_DEEPL_ENABLED") { data, err = TranslateDeepl(to, from, text) - } else if engine == "watson" { + } else if engine == "watson" && EnvTrueNoExist("MOZHI_WATSON_ENABLED") { data, err = TranslateWatson(to, from, text) - } else if engine == "yandex" { + } else if engine == "yandex" && EnvTrueNoExist("MOZHI_YANDEX_ENABLED") { data, err = TranslateYandex(to, from, text) - } else if engine == "mymemory" { + } else if engine == "mymemory" && EnvTrueNoExist("MOZHI_MYMEMORY_ENABLED") { data, err = TranslateMyMemory(to, from, text) - } else if engine == "duckduckgo" { + } else if engine == "duckduckgo" && EnvTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") { data, err = TranslateDuckDuckGo(to, from, text) } else { - return LangOut{}, errors.New("Engine does not exist.") + return LangOut{}, errors.New("Engine does not exist or has been disabled.") } if err != nil { 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) { var err error var data []byte - if engine == "google" { + if engine == "google" && EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") { data, err = TTSGoogle(lang, text) - } else if engine == "reverso" { + } else if engine == "reverso" && EnvTrueNoExist("MOZHI_REVERSO_ENABLED") { data, err = TTSReverso(lang, text) } 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 { return []byte(""), err