mozhi/pages/index.go

144 lines
4.2 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 {
2023-09-20 16:04:20 +05:30
engines := map[string]string{"all":"All Engines", "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-09-20 16:04:20 +05:30
// DeduplicateLists deduplicates a slice of List based on the Id field
func deDuplicateLists(input []libmozhi.List) []libmozhi.List {
// Create a map to store unique Ids
uniqueIds := make(map[string]struct{})
result := []libmozhi.List{}
// Iterate over the input slice
for _, item := range input {
// Check if the Id is unique
if _, found := uniqueIds[item.Id]; !found {
// Add the Id to the map and append the List to the result slice
uniqueIds[item.Id] = struct{}{}
result = append(result, item)
}
}
return result
}
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
sl := []libmozhi.List{}
tl := []libmozhi.List{}
for key, _ := range engines {
temp, _ := libmozhi.LangList(key, "sl")
temp2, _ := libmozhi.LangList(key, "tl")
sl = append(sl, temp...)
tl = append(tl, temp2...)
}
return deDuplicateLists(sl), deDuplicateLists(tl)
}
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-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
}
originalText = c.Query("text")
to := c.Query("to")
from := c.Query("from")
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" {
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-09-20 16:04:20 +05:30
} else {
translationExists = false
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,
2023-09-20 16:04:20 +05:30
"TranslationExists": translationExists,
"TranslateAll": transall,
2023-09-10 17:27:22 +05:30
"From": from,
"To": to,
"TtsFrom": ttsFrom,
"TtsTo": ttsTo,
"defaultLang": "en",
2023-06-25 00:10:57 +05:30
})
}