~yerinalexey/gtranslate

ref: 10f60675807902cd843aad35ec06051e20d99667 gtranslate/web.go -rw-r--r-- 728 bytes
10f60675Alexey Yerin Rename template-dir to templates-dir 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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) {
		if req.Method == "GET" {
			tmpl.ExecuteTemplate(w, "index", nil)
			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)
		}

		type indexPageData struct {
			From        string
			To          string
			Text        string
			Translation string
		}

		tmpl.ExecuteTemplate(w, "index", indexPageData{from, to, text, translation})
	}
}