Simple translation frontend, forked from https://git.sr.ht/~yerinalexey/gtranslate.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

42 lines
1006 B

package main
import (
"html/template"
"net/http"
"strings"
)
func CreateWebHandler(tmpl *template.Template, settings *TranslateSettings) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
type indexPageData struct {
Languages map[string]string
From string
To string
Text string
Translation string
}
if req.Method == "GET" {
tmpl.ExecuteTemplate(w, "index", indexPageData{Languages, "auto", "en", "", ""})
return
}
from := req.FormValue("from")
to := req.FormValue("to")
text := req.FormValue("text")
if strings.TrimSpace(text) == "" {
tmpl.ExecuteTemplate(w, "index", indexPageData{Languages, from, to, text, ""})
return
}
translation, err := Translate(settings, from, to, text)
if err != nil {
w.WriteHeader(500)
tmpl.ExecuteTemplate(w, "error", err)
return
}
tmpl.ExecuteTemplate(w, "index", indexPageData{Languages, from, to, text, translation})
}
}