2023-08-15 21:41:02 +05:30
package utils
2023-08-27 19:48:56 +05:30
import (
"errors"
2023-08-30 15:26:20 +05:30
"os"
2023-08-27 19:48:56 +05:30
)
2023-08-15 21:41:02 +05:30
type List struct {
Name string
Id string
}
2023-08-27 19:48:56 +05:30
type LangOut struct {
Engine string ` json:"engine" `
AutoDetect string ` json:"detected" `
OutputText string ` json:"translated-text" `
SourceLang string ` json:"source_language" `
TargetLang string ` json:"target_language" `
}
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" {
2023-08-28 20:41:57 +05:30
data = LangListGoogle ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "libre" {
2023-08-30 15:26:20 +05:30
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" )
}
2023-08-28 20:41:57 +05:30
data = LangListLibreTranslate ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "reverso" {
2023-08-28 20:41:57 +05:30
data = LangListReverso ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "deepl" {
2023-08-28 20:41:57 +05:30
data = LangListDeepl ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "watson" {
2023-08-28 20:41:57 +05:30
data = LangListWatson ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "yandex" {
2023-08-28 20:41:57 +05:30
data = LangListYandex ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "mymemory" {
2023-08-28 20:41:57 +05:30
data = LangListMyMemory ( listType )
2023-08-27 19:48:56 +05:30
} else if engine == "duckduckgo" {
2023-08-28 20:41:57 +05:30
data = LangListDuckDuckGo ( listType )
2023-08-27 19:48:56 +05:30
} else {
2023-08-27 19:54:30 +05:30
return [ ] List { } , errors . New ( "Engine does not exist." )
2023-08-27 19:48:56 +05:30
}
return data , nil
}
// General function to translate stuff so there is no need for a huge if-block everywhere
func Translate ( engine string , to string , from string , text string ) ( LangOut , error ) {
var err error
var data LangOut
if engine == "google" {
data , err = TranslateGoogle ( to , from , text )
} else if engine == "libre" {
2023-08-30 15:26:20 +05:30
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" )
}
2023-08-27 19:48:56 +05:30
data , err = TranslateLibreTranslate ( to , from , text )
} else if engine == "reverso" {
data , err = TranslateReverso ( to , from , text )
} else if engine == "deepl" {
data , err = TranslateDeepl ( to , from , text )
} else if engine == "watson" {
data , err = TranslateWatson ( to , from , text )
} else if engine == "yandex" {
data , err = TranslateYandex ( to , from , text )
} else if engine == "mymemory" {
data , err = TranslateMyMemory ( to , from , text )
} else if engine == "duckduckgo" {
data , err = TranslateDuckDuckGo ( to , from , text )
} else {
return LangOut { } , errors . New ( "Engine does not exist." )
}
if err != nil {
return LangOut { } , err
}
return data , nil
}
func TTS ( engine string , lang string , text string ) ( [ ] byte , error ) {
var err error
var data [ ] byte
if engine == "google" {
data , err = TTSGoogle ( lang , text )
} else if engine == "reverso" {
data , err = TTSReverso ( lang , text )
} else {
return [ ] byte ( "" ) , errors . New ( "Engine does not exist and/or doesn't support TTS." )
}
if err != nil {
return [ ] byte ( "" ) , err
}
return data , nil
}