mozhi/pages/index.go

172 lines
5.2 KiB
Go
Raw Normal View History

2023-06-24 18:40:57 +00:00
package pages
import (
"os"
"slices"
"strings"
2023-09-10 11:57:22 +00:00
"codeberg.org/aryak/libmozhi"
2023-10-11 10:27:28 +00:00
"codeberg.org/aryak/mozhi/utils"
2023-09-10 12:34:08 +00:00
"github.com/gofiber/fiber/v2"
2023-06-24 18:40:57 +00:00
)
func langListMerge(engines []string) ([]libmozhi.List, []libmozhi.List) {
2023-09-20 10:34:20 +00:00
sl := []libmozhi.List{}
tl := []libmozhi.List{}
for _, key := range engines {
2023-09-20 10:34:20 +00:00
temp, _ := libmozhi.LangList(key, "sl")
temp2, _ := libmozhi.LangList(key, "tl")
sl = append(sl, temp...)
tl = append(tl, temp2...)
}
return utils.DeDuplicateLists(sl), utils.DeDuplicateLists(tl)
2023-09-20 10:34:20 +00:00
}
2023-06-24 18:40:57 +00:00
func HandleIndex(c *fiber.Ctx) error {
engines := utils.EngineList()
type enginesStruct struct {
Engines []string `query:"engines"`
}
enginesSome := new(enginesStruct)
c.QueryParser(enginesSome)
var enginesAsArray []string
for engine := range engines {
enginesAsArray = append(enginesAsArray, engine)
2023-09-10 11:57:22 +00:00
}
engineCookie := c.Cookies("engine")
fromCookie := c.Cookies("from")
toCookie := c.Cookies("to")
2023-10-11 10:27:28 +00:00
var engine = utils.GetQueryOrFormValue(c, "engine")
if engine == "" || !slices.Contains(enginesAsArray, engine) {
if engineCookie != "" {
engine = engineCookie
} else {
engine = "google"
}
2023-07-22 14:24:15 +00:00
}
2023-09-20 10:34:20 +00:00
var sourceLanguages []libmozhi.List
var targetLanguages []libmozhi.List
if engine == "all" {
sourceLanguages, targetLanguages = langListMerge(enginesAsArray)
} else if engine == "some" {
if c.Cookies("engines") == "" && enginesSome.Engines == nil {
enginesSome.Engines = append(enginesSome.Engines, "google")
} else if enginesSome.Engines == nil && c.Cookies("engines") != "" {
enginesSome.Engines = strings.Split(c.Cookies("engines"), ",")
}
for i, engine := range enginesSome.Engines {
if !slices.Contains(enginesAsArray, engine) || engine == "some" {
// Delete array from slice if its not in engines list
enginesSome.Engines = append(enginesSome.Engines[:i], enginesSome.Engines[i+1:]...)
} else if engine == "all" {
enginesSome.Engines = enginesAsArray
}
}
if enginesSome.Engines == nil {
enginesSome.Engines = append(enginesSome.Engines, "google")
}
sourceLanguages, targetLanguages = langListMerge(enginesSome.Engines)
2023-09-20 10:34:20 +00:00
} else {
sourceLanguages, _ = libmozhi.LangList(engine, "sl")
targetLanguages, _ = libmozhi.LangList(engine, "tl")
2023-09-10 11:57:22 +00:00
}
2023-10-11 10:27:28 +00:00
originalText := utils.GetQueryOrFormValue(c, "text")
to := utils.GetQueryOrFormValue(c, "to")
from := utils.GetQueryOrFormValue(c, "from")
2024-01-17 12:55:58 +00:00
if from == "" {
from = utils.GetQueryOrFormValue(c, "sl")
if from == "" && fromCookie != "" {
from = fromCookie
}
2024-01-17 12:55:58 +00:00
}
if to == "" {
to = utils.GetQueryOrFormValue(c, "tl")
if to == "" && toCookie != "" {
to = toCookie
}
2024-01-17 12:55:58 +00:00
}
2023-09-10 11:57:22 +00:00
var translation libmozhi.LangOut
2023-09-20 10:34:20 +00:00
var translationExists bool
var transmany []libmozhi.LangOut
2023-09-10 11:57:22 +00:00
var tlerr error
var ttsFrom string
var ttsTo string
if engine != "" && originalText != "" && from != "" && to != "" {
2023-09-20 10:34:20 +00:00
if engine == "all" {
transmany = libmozhi.TranslateAll(to, from, originalText)
} else if engine == "some" {
// The error doesn't really matter since it just checks if the engines are valid, which is already checked in the code at the beginning of the func
transmany, _ = libmozhi.TranslateSome(enginesSome.Engines, to, from, originalText)
2023-09-20 10:34:20 +00:00
} else {
translation, tlerr = libmozhi.Translate(engine, to, from, originalText)
if tlerr != nil {
return fiber.NewError(fiber.StatusInternalServerError, tlerr.Error())
}
translationExists = true
2023-09-10 11:57:22 +00:00
}
if engine == "google" || engine == "reverso" {
if from == "auto" && translation.AutoDetect != "" {
ttsFrom = "/api/tts?lang=" + translation.AutoDetect + "&engine=" + engine + "&text=" + originalText
2023-09-10 11:57:22 +00:00
} else {
2023-09-10 12:34:08 +00:00
ttsFrom = "/api/tts?lang=" + from + "&engine=" + engine + "&text=" + originalText
2023-09-10 11:57:22 +00:00
}
2023-09-10 12:34:08 +00:00
ttsTo = "/api/tts?lang=" + to + "&engine=" + engine + "&text=" + translation.OutputText
2023-09-10 11:57:22 +00:00
}
2023-09-20 10:34:20 +00:00
} else {
translationExists = false
2023-09-10 11:57:22 +00:00
}
defaultLang := os.Getenv("MOZHI_DEFAULT_SOURCE_LANG")
preferAutoDetect := os.Getenv("MOZHI_DEFAULT_PREFER_AUTODETECT")
defaultLangTarget := os.Getenv("MOZHI_DEFAULT_TARGET_LANG")
2024-06-27 15:08:29 +00:00
if defaultLang == "" || (preferAutoDetect == "true" && sourceLanguages[0].Id == "auto") {
defaultLang = "auto"
}
if defaultLangTarget == "" {
defaultLangTarget = "en"
}
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)
}
if enginesSome.Engines != nil {
cookie := new(fiber.Cookie)
cookie.Name = "engines"
cookie.Value = strings.Join(enginesSome.Engines, ",")
c.Cookie(cookie)
}
2023-06-24 18:40:57 +00:00
return c.Render("index", fiber.Map{
"Engine": engine,
"enginesNames": engines,
"SomeEngines": enginesSome.Engines,
"SomeEnginesStr": strings.Join(enginesSome.Engines, ","),
"SourceLanguages": sourceLanguages,
"TargetLanguages": targetLanguages,
"OriginalText": originalText,
"Translation": translation,
"TranslationExists": translationExists,
"TranslateMany": transmany,
"From": from,
"To": to,
"TtsFrom": ttsFrom,
"TtsTo": ttsTo,
"defaultLang": defaultLang,
"defaultLangTarget": defaultLangTarget,
2023-06-24 18:40:57 +00:00
})
}