~yerinalexey/gtranslate

bf49f944269ced7848aecf7372962a981f29785a — Kyle Anthony Williams 7 months ago 10f6067
Use <select> for language selection
4 files changed, 143 insertions(+), 12 deletions(-)

A languages.go
M main.go
M templates/index.html
M web.go
A languages.go => languages.go +111 -0
@@ 0,0 1,111 @@
// Language code to name map lifted from py-googletrans: https://github.com/ssut/py-googletrans
package main

var Languages = map[string]string {
    "af": "afrikaans",
    "sq": "albanian",
    "am": "amharic",
    "ar": "arabic",
    "hy": "armenian",
    "az": "azerbaijani",
    "eu": "basque",
    "be": "belarusian",
    "bn": "bengali",
    "bs": "bosnian",
    "bg": "bulgarian",
    "ca": "catalan",
    "ceb": "cebuano",
    "ny": "chichewa",
    "zh-CN": "chinese (simplified)",
    "co": "corsican",
    "hr": "croatian",
    "cs": "czech",
    "da": "danish",
    "nl": "dutch",
    "en": "english",
    "eo": "esperanto",
    "et": "estonian",
    "tl": "filipino",
    "fi": "finnish",
    "fr": "french",
    "fy": "frisian",
    "gl": "galician",
    "ka": "georgian",
    "de": "german",
    "el": "greek",
    "gu": "gujarati",
    "ht": "haitian creole",
    "ha": "hausa",
    "haw": "hawaiian",
    "iw": "hebrew",
    "he": "hebrew",
    "hi": "hindi",
    "hmn": "hmong",
    "hu": "hungarian",
    "is": "icelandic",
    "ig": "igbo",
    "id": "indonesian",
    "ga": "irish",
    "it": "italian",
    "ja": "japanese",
    "jw": "javanese",
    "kn": "kannada",
    "kk": "kazakh",
    "km": "khmer",
    "ko": "korean",
    "ku": "kurdish (kurmanji)",
    "ky": "kyrgyz",
    "lo": "lao",
    "la": "latin",
    "lv": "latvian",
    "lt": "lithuanian",
    "lb": "luxembourgish",
    "mk": "macedonian",
    "mg": "malagasy",
    "ms": "malay",
    "ml": "malayalam",
    "mt": "maltese",
    "mi": "maori",
    "mr": "marathi",
    "mn": "mongolian",
    "my": "myanmar (burmese)",
    "ne": "nepali",
    "no": "norwegian",
    "or": "odia",
    "ps": "pashto",
    "fa": "persian",
    "pl": "polish",
    "pt": "portuguese",
    "pa": "punjabi",
    "ro": "romanian",
    "ru": "russian",
    "sm": "samoan",
    "gd": "scots gaelic",
    "sr": "serbian",
    "st": "sesotho",
    "sn": "shona",
    "sd": "sindhi",
    "si": "sinhala",
    "sk": "slovak",
    "sl": "slovenian",
    "so": "somali",
    "es": "spanish",
    "su": "sundanese",
    "sw": "swahili",
    "sv": "swedish",
    "tg": "tajik",
    "ta": "tamil",
    "te": "telugu",
    "th": "thai",
    "tr": "turkish",
    "uk": "ukrainian",
    "ur": "urdu",
    "ug": "uyghur",
    "uz": "uzbek",
    "vi": "vietnamese",
    "cy": "welsh",
    "xh": "xhosa",
    "yi": "yiddish",
    "yo": "yoruba",
    "zu": "zulu",
}

M main.go => main.go +6 -1
@@ 22,6 22,7 @@ import (
	"net/http"
	"net/url"
	"path"
	"strings"

	"github.com/gorilla/mux"
	"github.com/spf13/pflag"


@@ 53,7 54,11 @@ func main() {

	settings := TranslateSettings{proxyUrl, *userAgent}

	tmpl := template.Must(template.ParseGlob(path.Join(*templatesDir, "*.html")))
	funcMap := template.FuncMap{
		"title": strings.Title,
	}

	tmpl := template.Must(template.New("index").Funcs(funcMap).ParseGlob(path.Join(*templatesDir, "*.html")))

	r := mux.NewRouter()


M templates/index.html => templates/index.html +16 -2
@@ 16,14 16,28 @@
          <label for="from" class="item form__label">
            Source language:
          </label>
          <input id="from" name="from" class="form__input" placeholder="en,de" value="{{.From}}">
          <select name="from" id="from" class="form__input">
            {{range $code, $name := $.Languages}}
            <option value="{{$code}}" {{if eq $code $.From}}selected{{end}}>
              {{$name|title}}
            </option>
            {{end}}
          </select>
          <!-- <input id="from" name="from" class="form__input" placeholder="en,de" value="{{.From}}"> -->
        </div>

        <div class="form__field">
          <label for="to" class="item form__label">
            Translate to:
          </label>
          <input id="to" name="to" class="form__input" placeholder="fr,ru" value="{{.To}}">
          <select name="to" id="to" class="form__input">
            {{range $code, $name := $.Languages}}
            <option value="{{$code}}" {{if eq $code $.To}}selected{{end}}>
              {{$name|title}}
            </option>
            {{end}}
          </select>
          <!-- <input id="to" name="to" class="form__input" placeholder="fr,ru" value="{{.To}}"> -->
        </div>

        <div class="form__field">

M web.go => web.go +10 -9
@@ 7,8 7,16 @@ import (

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", nil)
			tmpl.ExecuteTemplate(w, "index", indexPageData{Languages, "", "", "", ""})
			return
		}



@@ 21,13 29,6 @@ func CreateWebHandler(tmpl *template.Template, settings *TranslateSettings) func
			writeError(w, 500, err)
		}

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

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