Use <select> for language selection

This commit is contained in:
Kyle Anthony Williams 2021-02-04 09:05:43 -05:00 committed by Alexey Yerin
parent 10f6067580
commit bf49f94426
4 changed files with 143 additions and 12 deletions

111
languages.go Normal file
View File

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

View File

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

View File

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

19
web.go
View File

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