From 0e2be0cd320615ce496a5807e1f29316ed3f6092 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Wed, 30 Aug 2023 15:26:20 +0530 Subject: [PATCH] give an error if someone tries to use libre without url set --- cmd/translate.go | 13 +++++++------ utils/main.go | 7 +++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/translate.go b/cmd/translate.go index 0357049..57eb3db 100644 --- a/cmd/translate.go +++ b/cmd/translate.go @@ -54,13 +54,14 @@ var translateCmd = &cobra.Command{ } else { if err != nil { fmt.Println(err) + } else { + fmt.Println("Translated Text: " + data.OutputText) + if source == "auto" { + fmt.Println("Detected Language: " + data.AutoDetect) + } + fmt.Println("Source Language: " + data.SourceLang) + fmt.Println("Target Language: " + data.TargetLang) } - fmt.Println("Translated Text: " + data.OutputText) - if source == "auto" { - fmt.Println("Detected Language: " + data.AutoDetect) - } - fmt.Println("Source Language: " + data.SourceLang) - fmt.Println("Target Language: " + data.TargetLang) } } }, diff --git a/utils/main.go b/utils/main.go index 6da2394..4955e4f 100644 --- a/utils/main.go +++ b/utils/main.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "os" ) type List struct { @@ -24,6 +25,9 @@ func LangList(engine string, listType string) ([]List, error) { if engine == "google" { data = LangListGoogle(listType) } else if engine == "libre" { + if os.Getenv("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" { data = LangListReverso(listType) @@ -50,6 +54,9 @@ func Translate(engine string, to string, from string, text string) (LangOut, err if engine == "google" { data, err = TranslateGoogle(to, from, text) } else if engine == "libre" { + 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" { data, err = TranslateReverso(to, from, text)