2023-08-15 16:11:02 +00:00
package utils
2023-08-27 14:18:56 +00:00
import (
"errors"
2023-08-30 09:56:20 +00:00
"os"
2023-08-27 14:18:56 +00:00
)
2023-08-15 16:11:02 +00:00
type List struct {
Name string
Id string
}
2023-08-27 14:18:56 +00:00
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" `
}
2023-08-30 10:35:16 +00:00
func EnvTrueNoExist ( env string ) bool {
if _ , ok := os . LookupEnv ( env ) ; ok == false || os . Getenv ( env ) == "true" {
return true
}
return false
}
2023-08-27 14:18:56 +00:00
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." )
}
2023-08-30 10:35:16 +00:00
if engine == "google" && EnvTrueNoExist ( "MOZHI_GOOGLE_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListGoogle ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "libre" && EnvTrueNoExist ( "MOZHI_LIBRETRANSLATE_ENABLED" ) {
if EnvTrueNoExist ( "MOZHI_LIBRETRANSLATE_URL" ) {
2023-08-30 09:56:20 +00:00
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 15:11:57 +00:00
data = LangListLibreTranslate ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "reverso" && EnvTrueNoExist ( "MOZHI_REVERSO_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListReverso ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "deepl" && EnvTrueNoExist ( "MOZHI_DEEPL_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListDeepl ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "watson" && EnvTrueNoExist ( "MOZHI_WATSON_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListWatson ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "yandex" && EnvTrueNoExist ( "MOZHI_YANDEX_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListYandex ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "mymemory" && EnvTrueNoExist ( "MOZHI_MYMEMORY_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListMyMemory ( listType )
2023-08-30 10:35:16 +00:00
} else if engine == "duckduckgo" && EnvTrueNoExist ( "MOZHI_DUCKDUCKGO_ENABLED" ) {
2023-08-28 15:11:57 +00:00
data = LangListDuckDuckGo ( listType )
2023-08-27 14:18:56 +00:00
} else {
2023-08-30 10:35:16 +00:00
return [ ] List { } , errors . New ( "Engine does not exist or has been disabled." )
2023-08-27 14:18:56 +00:00
}
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
2023-08-30 10:35:16 +00:00
if engine == "google" && EnvTrueNoExist ( "MOZHI_GOOGLE_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateGoogle ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "libre" && EnvTrueNoExist ( "MOZHI_LIBRETRANSLATE_ENABLED" ) {
2023-08-30 09:56:20 +00:00
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 14:18:56 +00:00
data , err = TranslateLibreTranslate ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "reverso" && EnvTrueNoExist ( "MOZHI_REVERSO_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateReverso ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "deepl" && EnvTrueNoExist ( "MOZHI_DEEPL_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateDeepl ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "watson" && EnvTrueNoExist ( "MOZHI_WATSON_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateWatson ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "yandex" && EnvTrueNoExist ( "MOZHI_YANDEX_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateYandex ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "mymemory" && EnvTrueNoExist ( "MOZHI_MYMEMORY_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateMyMemory ( to , from , text )
2023-08-30 10:35:16 +00:00
} else if engine == "duckduckgo" && EnvTrueNoExist ( "MOZHI_DUCKDUCKGO_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TranslateDuckDuckGo ( to , from , text )
} else {
2023-08-30 10:35:16 +00:00
return LangOut { } , errors . New ( "Engine does not exist or has been disabled." )
2023-08-27 14:18:56 +00:00
}
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
2023-08-30 10:35:16 +00:00
if engine == "google" && EnvTrueNoExist ( "MOZHI_GOOGLE_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TTSGoogle ( lang , text )
2023-08-30 10:35:16 +00:00
} else if engine == "reverso" && EnvTrueNoExist ( "MOZHI_REVERSO_ENABLED" ) {
2023-08-27 14:18:56 +00:00
data , err = TTSReverso ( lang , text )
} else {
2023-08-30 10:35:16 +00:00
return [ ] byte ( "" ) , errors . New ( "Engine does not exist and/or doesn't support TTS and/or has been disabled." )
2023-08-27 14:18:56 +00:00
}
if err != nil {
return [ ] byte ( "" ) , err
}
return data , nil
}