2023-06-25 00:10:57 +05:30
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2023-10-05 18:59:22 +05:30
|
|
|
"os"
|
|
|
|
"slices"
|
|
|
|
|
2023-09-10 17:27:22 +05:30
|
|
|
"codeberg.org/aryak/libmozhi"
|
2023-10-11 15:57:28 +05:30
|
|
|
"codeberg.org/aryak/mozhi/utils"
|
2023-09-10 18:04:08 +05:30
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-06-25 00:10:57 +05:30
|
|
|
)
|
|
|
|
|
2023-09-20 16:04:20 +05:30
|
|
|
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
|
|
|
|
sl := []libmozhi.List{}
|
|
|
|
tl := []libmozhi.List{}
|
2024-02-20 14:41:45 +05:30
|
|
|
for key := range engines {
|
2023-09-20 16:04:20 +05:30
|
|
|
temp, _ := libmozhi.LangList(key, "sl")
|
|
|
|
temp2, _ := libmozhi.LangList(key, "tl")
|
|
|
|
sl = append(sl, temp...)
|
|
|
|
tl = append(tl, temp2...)
|
|
|
|
}
|
2023-10-25 18:39:31 +05:30
|
|
|
return utils.DeDuplicateLists(sl), utils.DeDuplicateLists(tl)
|
2023-09-20 16:04:20 +05:30
|
|
|
}
|
|
|
|
|
2023-06-25 00:10:57 +05:30
|
|
|
func HandleIndex(c *fiber.Ctx) error {
|
2023-10-25 18:39:31 +05:30
|
|
|
engines := utils.EngineList()
|
2023-10-05 18:59:22 +05:30
|
|
|
var enginesAsArray []string
|
|
|
|
for engine := range engines {
|
|
|
|
enginesAsArray = append(enginesAsArray, engine)
|
2023-09-10 17:27:22 +05:30
|
|
|
}
|
2024-04-04 12:07:12 +05:30
|
|
|
engineCookie := c.Cookies("engine")
|
|
|
|
fromCookie := c.Cookies("from")
|
|
|
|
toCookie := c.Cookies("to")
|
2023-10-11 15:57:28 +05:30
|
|
|
var engine = utils.GetQueryOrFormValue(c, "engine")
|
2023-10-05 18:59:22 +05:30
|
|
|
if engine == "" || !slices.Contains(enginesAsArray, engine) {
|
2024-04-04 12:07:12 +05:30
|
|
|
if engineCookie != "" {
|
|
|
|
engine = engineCookie
|
|
|
|
} else {
|
|
|
|
engine = "google"
|
|
|
|
}
|
2023-07-22 19:54:15 +05:30
|
|
|
}
|
|
|
|
|
2023-09-20 16:04:20 +05:30
|
|
|
var sourceLanguages []libmozhi.List
|
|
|
|
var targetLanguages []libmozhi.List
|
|
|
|
if engine == "all" {
|
|
|
|
sourceLanguages, targetLanguages = langListMerge(engines)
|
|
|
|
} else {
|
|
|
|
sourceLanguages, _ = libmozhi.LangList(engine, "sl")
|
|
|
|
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
2023-09-10 17:27:22 +05:30
|
|
|
}
|
|
|
|
|
2023-10-11 15:57:28 +05:30
|
|
|
originalText := utils.GetQueryOrFormValue(c, "text")
|
|
|
|
to := utils.GetQueryOrFormValue(c, "to")
|
|
|
|
from := utils.GetQueryOrFormValue(c, "from")
|
2024-01-17 18:25:58 +05:30
|
|
|
if from == "" {
|
|
|
|
from = utils.GetQueryOrFormValue(c, "sl")
|
2024-04-04 12:07:12 +05:30
|
|
|
if from == "" && fromCookie != "" {
|
|
|
|
from = fromCookie
|
|
|
|
}
|
2024-01-17 18:25:58 +05:30
|
|
|
}
|
|
|
|
if to == "" {
|
|
|
|
to = utils.GetQueryOrFormValue(c, "tl")
|
2024-04-04 12:07:12 +05:30
|
|
|
if to == "" && toCookie != "" {
|
|
|
|
to = toCookie
|
|
|
|
}
|
2024-01-17 18:25:58 +05:30
|
|
|
}
|
2023-10-05 18:59:22 +05:30
|
|
|
|
2023-09-10 17:27:22 +05:30
|
|
|
var translation libmozhi.LangOut
|
2023-09-20 16:04:20 +05:30
|
|
|
var translationExists bool
|
|
|
|
var transall []libmozhi.LangOut
|
2023-09-10 17:27:22 +05:30
|
|
|
var tlerr error
|
|
|
|
var ttsFrom string
|
|
|
|
var ttsTo string
|
|
|
|
if engine != "" && originalText != "" && from != "" && to != "" {
|
2023-09-20 16:04:20 +05:30
|
|
|
if engine == "all" {
|
|
|
|
transall = libmozhi.TranslateAll(to, from, originalText)
|
|
|
|
} else {
|
|
|
|
translation, tlerr = libmozhi.Translate(engine, to, from, originalText)
|
|
|
|
if tlerr != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, tlerr.Error())
|
|
|
|
}
|
|
|
|
translationExists = true
|
2023-09-10 17:27:22 +05:30
|
|
|
}
|
|
|
|
if engine == "google" || engine == "reverso" {
|
2023-12-04 19:17:19 +05:30
|
|
|
if from == "auto" && translation.AutoDetect != "" {
|
|
|
|
ttsFrom = "/api/tts?lang=" + translation.AutoDetect + "&engine=" + engine + "&text=" + originalText
|
2023-09-10 17:27:22 +05:30
|
|
|
} 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-09-20 16:04:20 +05:30
|
|
|
} else {
|
|
|
|
translationExists = false
|
2023-09-10 17:27:22 +05:30
|
|
|
}
|
2023-10-20 15:49:51 +05:30
|
|
|
|
|
|
|
defaultLang := os.Getenv("MOZHI_DEFAULT_SOURCE_LANG")
|
|
|
|
defaultLangTarget := os.Getenv("MOZHI_DEFAULT_TARGET_LANG")
|
|
|
|
if defaultLang == "" {
|
|
|
|
defaultLang = "auto"
|
|
|
|
}
|
|
|
|
if defaultLangTarget == "" {
|
|
|
|
defaultLangTarget = "en"
|
|
|
|
}
|
2024-04-04 12:07:12 +05:30
|
|
|
cookie := new(fiber.Cookie)
|
|
|
|
cookie.Name = "engine"
|
|
|
|
cookie.Value = engine
|
|
|
|
c.Cookie(cookie)
|
|
|
|
if from != "" {
|
|
|
|
cookie := new(fiber.Cookie)
|
|
|
|
cookie.Name = "from"
|
|
|
|
cookie.Value = from
|
|
|
|
c.Cookie(cookie)
|
|
|
|
}
|
|
|
|
if to != "" {
|
|
|
|
cookie := new(fiber.Cookie)
|
|
|
|
cookie.Name = "to"
|
|
|
|
cookie.Value = to
|
|
|
|
c.Cookie(cookie)
|
|
|
|
}
|
2023-06-25 00:10:57 +05:30
|
|
|
return c.Render("index", fiber.Map{
|
2023-10-05 18:59:22 +05:30
|
|
|
"Engine": engine,
|
|
|
|
"enginesNames": engines,
|
|
|
|
"SourceLanguages": sourceLanguages,
|
|
|
|
"TargetLanguages": targetLanguages,
|
|
|
|
"OriginalText": originalText,
|
|
|
|
"Translation": translation,
|
|
|
|
"TranslationExists": translationExists,
|
|
|
|
"TranslateAll": transall,
|
|
|
|
"From": from,
|
|
|
|
"To": to,
|
|
|
|
"TtsFrom": ttsFrom,
|
|
|
|
"TtsTo": ttsTo,
|
2023-10-20 15:49:51 +05:30
|
|
|
"defaultLang": defaultLang,
|
|
|
|
"defaultLangTarget": defaultLangTarget,
|
2023-06-25 00:10:57 +05:30
|
|
|
})
|
|
|
|
}
|