From 5984fdf1d95674f8a379b30f1c7dc2f5a35a6b68 Mon Sep 17 00:00:00 2001 From: Arya Kiran Date: Fri, 20 Oct 2023 17:34:41 +0530 Subject: [PATCH] make switchlanguage swap text (closes #10) --- serve/serve.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/serve/serve.go b/serve/serve.go index b80c782..d1b638a 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -7,6 +7,7 @@ import ( "os" "runtime" + "codeberg.org/aryak/libmozhi" _ "codeberg.org/aryak/mozhi/docs" "codeberg.org/aryak/mozhi/pages" "codeberg.org/aryak/mozhi/public" @@ -102,8 +103,17 @@ func Serve(port string) { engine := utils.Sanitize(utils.GetQueryOrFormValue(c, "engine"), "alpha") from := utils.Sanitize(utils.GetQueryOrFormValue(c, "from"), "alpha") to := utils.Sanitize(utils.GetQueryOrFormValue(c, "to"), "alpha") - text := utils.Sanitize(utils.GetQueryOrFormValue(c, "text"), "alpha") - return c.Redirect("/?engine="+engine+"&from="+to+"&to="+from+"&text="+text+"&redirected=true", 301) + text := utils.GetQueryOrFormValue(c, "text") + var swapText string + if engine != "all" && text != "" { + translation, tlerr := libmozhi.Translate(engine, to, from, text) + if tlerr == nil { + swapText = translation.OutputText + } else { + swapText = text + } + } + return c.Redirect("/?engine="+engine+"&from="+to+"&to="+from+"&text="+swapText+"&redirected=true", 301) }) app.Get("/about", pages.HandleAbout) app.Use("/", filesystem.New(filesystem.Config{