~yerinalexey/gtranslate

ref: bf49f944269ced7848aecf7372962a981f29785a gtranslate/web.go -rw-r--r-- 809 bytes
bf49f944 — Kyle Anthony Williams Use <select> for language selection 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
34
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)
		}

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