add selectable engines support to web (closes #20)

This commit is contained in:
Arya Kiran 2024-08-27 22:36:11 +05:30
parent a2ac174314
commit 0c2c0f7dfd
No known key found for this signature in database
GPG key ID: 842D12BDA50DF120
5 changed files with 78 additions and 19 deletions

View file

@ -2,8 +2,8 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"strings"
"fmt" "fmt"
"strings"
"codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
@ -12,7 +12,7 @@ import (
var ( var (
engine string engine string
engines string engines string
query string query string
source string source string
dest string dest string

View file

@ -3,16 +3,17 @@ package pages
import ( import (
"os" "os"
"slices" "slices"
"strings"
"codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) { func langListMerge(engines []string) ([]libmozhi.List, []libmozhi.List) {
sl := []libmozhi.List{} sl := []libmozhi.List{}
tl := []libmozhi.List{} tl := []libmozhi.List{}
for key := range engines { for _, key := range engines {
temp, _ := libmozhi.LangList(key, "sl") temp, _ := libmozhi.LangList(key, "sl")
temp2, _ := libmozhi.LangList(key, "tl") temp2, _ := libmozhi.LangList(key, "tl")
sl = append(sl, temp...) sl = append(sl, temp...)
@ -23,6 +24,11 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List)
func HandleIndex(c *fiber.Ctx) error { func HandleIndex(c *fiber.Ctx) error {
engines := utils.EngineList() engines := utils.EngineList()
type enginesStruct struct {
Engines []string `query:"engines"`
}
enginesSome := new(enginesStruct)
c.QueryParser(enginesSome)
var enginesAsArray []string var enginesAsArray []string
for engine := range engines { for engine := range engines {
enginesAsArray = append(enginesAsArray, engine) enginesAsArray = append(enginesAsArray, engine)
@ -42,7 +48,25 @@ func HandleIndex(c *fiber.Ctx) error {
var sourceLanguages []libmozhi.List var sourceLanguages []libmozhi.List
var targetLanguages []libmozhi.List var targetLanguages []libmozhi.List
if engine == "all" { if engine == "all" {
sourceLanguages, targetLanguages = langListMerge(engines) 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)
} else { } else {
sourceLanguages, _ = libmozhi.LangList(engine, "sl") sourceLanguages, _ = libmozhi.LangList(engine, "sl")
targetLanguages, _ = libmozhi.LangList(engine, "tl") targetLanguages, _ = libmozhi.LangList(engine, "tl")
@ -66,13 +90,16 @@ func HandleIndex(c *fiber.Ctx) error {
var translation libmozhi.LangOut var translation libmozhi.LangOut
var translationExists bool var translationExists bool
var transall []libmozhi.LangOut var transmany []libmozhi.LangOut
var tlerr error var tlerr error
var ttsFrom string var ttsFrom string
var ttsTo string var ttsTo string
if engine != "" && originalText != "" && from != "" && to != "" { if engine != "" && originalText != "" && from != "" && to != "" {
if engine == "all" { if engine == "all" {
transall = libmozhi.TranslateAll(to, from, originalText) 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)
} else { } else {
translation, tlerr = libmozhi.Translate(engine, to, from, originalText) translation, tlerr = libmozhi.Translate(engine, to, from, originalText)
if tlerr != nil { if tlerr != nil {
@ -117,15 +144,23 @@ func HandleIndex(c *fiber.Ctx) error {
cookie.Value = to cookie.Value = to
c.Cookie(cookie) c.Cookie(cookie)
} }
if enginesSome.Engines != nil {
cookie := new(fiber.Cookie)
cookie.Name = "engines"
cookie.Value = strings.Join(enginesSome.Engines, ",")
c.Cookie(cookie)
}
return c.Render("index", fiber.Map{ return c.Render("index", fiber.Map{
"Engine": engine, "Engine": engine,
"enginesNames": engines, "enginesNames": engines,
"SomeEngines": enginesSome.Engines,
"SomeEnginesStr": strings.Join(enginesSome.Engines, ","),
"SourceLanguages": sourceLanguages, "SourceLanguages": sourceLanguages,
"TargetLanguages": targetLanguages, "TargetLanguages": targetLanguages,
"OriginalText": originalText, "OriginalText": originalText,
"Translation": translation, "Translation": translation,
"TranslationExists": translationExists, "TranslationExists": translationExists,
"TranslateAll": transall, "TranslateMany": transmany,
"From": from, "From": from,
"To": to, "To": to,
"TtsFrom": ttsFrom, "TtsFrom": ttsFrom,

View file

@ -17,6 +17,7 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/filesystem" "github.com/gofiber/fiber/v2/middleware/filesystem"
//"github.com/gofiber/fiber/v2/middleware/limiter" //"github.com/gofiber/fiber/v2/middleware/limiter"
// For debugging purposes // For debugging purposes
// "github.com/gofiber/fiber/v2/middleware/logger" // "github.com/gofiber/fiber/v2/middleware/logger"
@ -37,11 +38,19 @@ func Serve(port string) {
views := http.FS(views.GetFiles()) views := http.FS(views.GetFiles())
engine := html.NewFileSystem(views, ".html") engine := html.NewFileSystem(views, ".html")
engine.AddFunc( engine.AddFunc("newlinetobr", func(s string) template.HTML {
"newlinetobr", func(s string) template.HTML { return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "<br>"), "\r", ""))
return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "<br>"), "\r", "")) })
},
) // Returns specific string if a string is in another slice
engine.AddFunc("contains", func(data []string, s string, retval string) template.HTML {
for _, val := range data {
if val == s {
return template.HTML(retval)
}
}
return template.HTML("")
})
app := fiber.New(fiber.Config{ app := fiber.New(fiber.Config{
Views: engine, Views: engine,
@ -107,7 +116,7 @@ func Serve(port string) {
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha") to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha")
text := utils.GetQueryOrFormValue(c, "text") text := utils.GetQueryOrFormValue(c, "text")
var swapText string var swapText string
if engine != "all" && text != "" { if engine != "all" && engine != "some" && text != "" {
translation, tlerr := libmozhi.Translate(engine, to, from, text) translation, tlerr := libmozhi.Translate(engine, to, from, text)
if tlerr == nil { if tlerr == nil {
swapText = translation.OutputText swapText = translation.OutputText

View file

@ -1,10 +1,11 @@
package utils package utils
import ( import (
"codeberg.org/aryak/libmozhi"
"github.com/gofiber/fiber/v2"
"os" "os"
"regexp" "regexp"
"codeberg.org/aryak/libmozhi"
"github.com/gofiber/fiber/v2"
) )
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z,]+`) var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z,]+`)
@ -34,7 +35,7 @@ func Sanitize(str string, strip string) string {
} }
func EngineList() map[string]string { func EngineList() map[string]string {
engines := map[string]string{"all": "All Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "yandex": "Yandex"} engines := map[string]string{"all": "All Engines", "some": "Some Engines", "google": "Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "yandex": "Yandex"}
if EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") == false { if EnvTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
delete(engines, "google") delete(engines, "google")
} else if EnvTrueNoExist("MOZHI_DEEPL_ENABLED") == false { } else if EnvTrueNoExist("MOZHI_DEEPL_ENABLED") == false {

View file

@ -14,9 +14,23 @@
</div> </div>
<br /> <br />
<br /> <br />
{{ if eq .Engine "some" }}
<form action="/" method="get" id="enginesForm">
<select name="engines" aria-label="Engines you want to use" id="engines" multiple>
{{ range $key, $value := .enginesNames}}
<option value="{{ $key }}" {{ contains $.SomeEngines $key "selected" }}>
{{ $value }}
</option>
{{end}}
</select>
<button type="submit">Select Engines</button>
{{ end }}
<form action="/" method="post" id="translation-form"> <form action="/" method="post" id="translation-form">
<!-- This hidden input is so that the engine gets sent in the request even though its not declared here --> <!-- This hidden input is so that the engine gets sent in the request even though its not declared here -->
<input name="engine" value="{{.Engine}}" type="hidden" /> <input name="engine" value="{{.Engine}}" type="hidden" />
{{ if eq .Engine "some" }}
<input name="engines" value="{{.EnginesNamesStr}}" type="hidden" />
{{ end }}
<div class="wrap languages center-area"> <div class="wrap languages center-area">
<div class="language"> <div class="language">
<select name="from" aria-label="Source language" id="sourceLanguage"> <select name="from" aria-label="Source language" id="sourceLanguage">
@ -74,8 +88,8 @@
{{ end }} {{ end }}
<br> <br>
</div> </div>
{{ if .TranslateAll }} {{ if .TranslateMany }}
{{ range $key, $value := .TranslateAll }} {{ range $key, $value := .TranslateMany }}
<div class="item-wrapper center-area2"> <div class="item-wrapper center-area2">
Engine: {{.Engine}} Engine: {{.Engine}}
<textarea class="translation item" dir="auto" placeholder="Translation" id="output" readonly> <textarea class="translation item" dir="auto" placeholder="Translation" id="output" readonly>