mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-08 14:52:24 +05:30
add selectable engines support to web (closes #20)
This commit is contained in:
parent
a2ac174314
commit
0c2c0f7dfd
@ -2,8 +2,8 @@ package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/aryak/libmozhi"
|
||||
|
||||
@ -12,7 +12,7 @@ import (
|
||||
|
||||
var (
|
||||
engine string
|
||||
engines string
|
||||
engines string
|
||||
query string
|
||||
source string
|
||||
dest string
|
||||
|
@ -3,16 +3,17 @@ package pages
|
||||
import (
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"codeberg.org/aryak/libmozhi"
|
||||
"codeberg.org/aryak/mozhi/utils"
|
||||
"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{}
|
||||
tl := []libmozhi.List{}
|
||||
for key := range engines {
|
||||
for _, key := range engines {
|
||||
temp, _ := libmozhi.LangList(key, "sl")
|
||||
temp2, _ := libmozhi.LangList(key, "tl")
|
||||
sl = append(sl, temp...)
|
||||
@ -23,6 +24,11 @@ func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List)
|
||||
|
||||
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)
|
||||
@ -42,7 +48,25 @@ func HandleIndex(c *fiber.Ctx) error {
|
||||
var sourceLanguages []libmozhi.List
|
||||
var targetLanguages []libmozhi.List
|
||||
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 {
|
||||
sourceLanguages, _ = libmozhi.LangList(engine, "sl")
|
||||
targetLanguages, _ = libmozhi.LangList(engine, "tl")
|
||||
@ -66,13 +90,16 @@ func HandleIndex(c *fiber.Ctx) error {
|
||||
|
||||
var translation libmozhi.LangOut
|
||||
var translationExists bool
|
||||
var transall []libmozhi.LangOut
|
||||
var transmany []libmozhi.LangOut
|
||||
var tlerr error
|
||||
var ttsFrom string
|
||||
var ttsTo string
|
||||
if engine != "" && originalText != "" && from != "" && to != "" {
|
||||
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 {
|
||||
translation, tlerr = libmozhi.Translate(engine, to, from, originalText)
|
||||
if tlerr != nil {
|
||||
@ -117,15 +144,23 @@ func HandleIndex(c *fiber.Ctx) error {
|
||||
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)
|
||||
}
|
||||
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,
|
||||
"TranslateAll": transall,
|
||||
"TranslateMany": transmany,
|
||||
"From": from,
|
||||
"To": to,
|
||||
"TtsFrom": ttsFrom,
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/compress"
|
||||
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
||||
|
||||
//"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||
// For debugging purposes
|
||||
// "github.com/gofiber/fiber/v2/middleware/logger"
|
||||
@ -37,11 +38,19 @@ func Serve(port string) {
|
||||
views := http.FS(views.GetFiles())
|
||||
engine := html.NewFileSystem(views, ".html")
|
||||
|
||||
engine.AddFunc(
|
||||
"newlinetobr", func(s string) template.HTML {
|
||||
return template.HTML(strings.ReplaceAll(strings.ReplaceAll(s, "\n", "<br>"), "\r", ""))
|
||||
},
|
||||
)
|
||||
engine.AddFunc("newlinetobr", func(s string) template.HTML {
|
||||
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{
|
||||
Views: engine,
|
||||
@ -107,7 +116,7 @@ func Serve(port string) {
|
||||
to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha")
|
||||
text := utils.GetQueryOrFormValue(c, "text")
|
||||
var swapText string
|
||||
if engine != "all" && text != "" {
|
||||
if engine != "all" && engine != "some" && text != "" {
|
||||
translation, tlerr := libmozhi.Translate(engine, to, from, text)
|
||||
if tlerr == nil {
|
||||
swapText = translation.OutputText
|
||||
|
@ -1,10 +1,11 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"codeberg.org/aryak/libmozhi"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"os"
|
||||
"regexp"
|
||||
|
||||
"codeberg.org/aryak/libmozhi"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z,]+`)
|
||||
@ -34,7 +35,7 @@ func Sanitize(str string, strip 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 {
|
||||
delete(engines, "google")
|
||||
} else if EnvTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
|
||||
|
@ -14,9 +14,23 @@
|
||||
</div>
|
||||
<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">
|
||||
<!-- 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" />
|
||||
{{ if eq .Engine "some" }}
|
||||
<input name="engines" value="{{.EnginesNamesStr}}" type="hidden" />
|
||||
{{ end }}
|
||||
<div class="wrap languages center-area">
|
||||
<div class="language">
|
||||
<select name="from" aria-label="Source language" id="sourceLanguage">
|
||||
@ -74,8 +88,8 @@
|
||||
{{ end }}
|
||||
<br>
|
||||
</div>
|
||||
{{ if .TranslateAll }}
|
||||
{{ range $key, $value := .TranslateAll }}
|
||||
{{ if .TranslateMany }}
|
||||
{{ range $key, $value := .TranslateMany }}
|
||||
<div class="item-wrapper center-area2">
|
||||
Engine: {{.Engine}}
|
||||
<textarea class="translation item" dir="auto" placeholder="Translation" id="output" readonly>
|
||||
|
Loading…
Reference in New Issue
Block a user