Use <select> for language selection

master
Kyle Anthony Williams 2 years ago committed by Alexey Yerin
parent 10f6067580
commit bf49f94426
  1. 111
      languages.go
  2. 7
      main.go
  3. 18
      templates/index.html
  4. 19
      web.go

@ -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",
}

@ -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()

@ -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">

@ -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})
}
}

Loading…
Cancel
Save