mozhi/pages/index.go

151 lines
4.6 KiB
Go
Raw Normal View History

2023-06-25 00:10:57 +05:30
package pages
import (
"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
)
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{"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()
var enginesAsArray []string
for engine := range engines {
enginesAsArray = append(enginesAsArray, engine)
2023-09-10 17:27:22 +05:30
}
2023-10-11 15:57:28 +05:30
var engine = utils.GetQueryOrFormValue(c, "engine")
if engine == "" || !slices.Contains(enginesAsArray, 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
}
2023-10-11 15:57:28 +05:30
originalText := utils.GetQueryOrFormValue(c, "text")
to := utils.GetQueryOrFormValue(c, "to")
from := utils.GetQueryOrFormValue(c, "from")
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" {
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
}
defaultLang := os.Getenv("MOZHI_DEFAULT_SOURCE_LANG")
defaultLangTarget := os.Getenv("MOZHI_DEFAULT_TARGET_LANG")
if defaultLang == "" {
defaultLang = "auto"
}
if defaultLangTarget == "" {
defaultLangTarget = "en"
}
2023-06-25 00:10:57 +05:30
return c.Render("index", fiber.Map{
"Engine": engine,
"enginesNames": engines,
"SourceLanguages": sourceLanguages,
"TargetLanguages": targetLanguages,
"OriginalText": originalText,
"Translation": translation,
"TranslationExists": translationExists,
"TranslateAll": transall,
"From": from,
"To": to,
"TtsFrom": ttsFrom,
"TtsTo": ttsTo,
"defaultLang": defaultLang,
"defaultLangTarget": defaultLangTarget,
2023-06-25 00:10:57 +05:30
})
}