36 lines
819 B
Go
36 lines
819 B
Go
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
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, "", "", "", ""})
|
|
return
|
|
}
|
|
|
|
from := req.FormValue("from")
|
|
to := req.FormValue("to")
|
|
text := req.FormValue("text")
|
|
|
|
translation, err := Translate(settings, from, to, text)
|
|
if err != nil {
|
|
writeError(w, 500, err)
|
|
return
|
|
}
|
|
|
|
tmpl.ExecuteTemplate(w, "index", indexPageData{Languages, from, to, text, translation})
|
|
}
|
|
}
|