mozhi/pages/index.go

98 lines
3.0 KiB
Go
Raw Normal View History

2023-06-25 00:10:57 +05:30
package pages
import (
2023-09-10 17:27:22 +05:30
"codeberg.org/aryak/libmozhi"
2023-09-10 18:04:08 +05:30
"github.com/gofiber/fiber/v2"
"os"
2023-06-25 00:10:57 +05:30
)
func envTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
}
func engineList() map[string]string {
engines := map[string]string{"google":"Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"}
if envTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
delete(engines,"google")
} else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
delete(engines,"deepl")
} else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
delete(engines,"duckduckgo")
} else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
delete(engines,"libre")
} else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
delete(engines,"mymemory")
} else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
delete(engines,"reverso")
} else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false {
delete(engines,"watson")
} else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
delete(engines,"yandex")
}
return engines
}
2023-06-25 00:10:57 +05:30
func HandleIndex(c *fiber.Ctx) error {
engines := engineList()
2023-09-10 17:27:22 +05:30
var engine string
var originalText string
if c.Query("engine") == "" {
engine = "google"
}
if c.Query("engine") != "" {
for key, _ := range engines {
if c.Query("engine") == key {
2023-09-10 17:27:22 +05:30
engine = c.Query("engine")
}
2023-09-10 18:04:08 +05:30
}
2023-09-10 17:27:22 +05:30
if engine == "" {
engine = "google"
}
2023-07-22 19:54:15 +05:30
}
2023-09-10 17:27:22 +05:30
sourceLanguages, err1 := libmozhi.LangList(engine, "sl")
targetLanguages, err2 := libmozhi.LangList(engine, "tl")
if err1 != nil || err2 != nil {
return fiber.NewError(fiber.StatusInternalServerError, err1.Error()+err2.Error())
}
originalText = c.Query("text")
to := c.Query("to")
from := c.Query("from")
var translation libmozhi.LangOut
var tlerr error
var ttsFrom string
var ttsTo string
if engine != "" && originalText != "" && from != "" && to != "" {
translation, tlerr = libmozhi.Translate(engine, to, from, originalText)
if tlerr != nil {
return fiber.NewError(fiber.StatusInternalServerError, tlerr.Error())
}
if engine == "google" || engine == "reverso" {
if from == "auto" {
//ttsFrom = "/api/tts?lang="+translation.AutoDetect+"&engine="+engine+"&text="+originalText
ttsFrom = ""
} else {
2023-09-10 18:04:08 +05:30
ttsFrom = "/api/tts?lang=" + from + "&engine=" + engine + "&text=" + originalText
2023-09-10 17:27:22 +05:30
}
2023-09-10 18:04:08 +05:30
ttsTo = "/api/tts?lang=" + to + "&engine=" + engine + "&text=" + translation.OutputText
2023-09-10 17:27:22 +05:30
}
}
2023-06-25 00:10:57 +05:30
return c.Render("index", fiber.Map{
2023-09-10 17:27:22 +05:30
"Engine": engine,
"enginesNames": engines,
"SourceLanguages": sourceLanguages,
"TargetLanguages": targetLanguages,
"OriginalText": originalText,
"Translation": translation,
"From": from,
"To": to,
"TtsFrom": ttsFrom,
"TtsTo": ttsTo,
"defaultLang": "en",
2023-06-25 00:10:57 +05:30
})
}