~yerinalexey/public-inbox

gtranslate: Added language map to package. v1 NEEDS REVISION

Kyle Anthony Williams
Kyle Anthony Williams: 3
 Added language map to package.
 Replaced language selector.
 Added title casing to language selectors.

 5 files changed, 138 insertions(+), 14 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~yerinalexey/public-inbox/patches/19984/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gtranslate 1/3] Added language map to package. Export this patch

Kyle Anthony Williams
---
 languages.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 112 insertions(+)
 create mode 100644 languages.go

diff --git a/languages.go b/languages.go
new file mode 100644
index 0000000..4c1d884
--- /dev/null
+++ b/languages.go
@@ -0,0 +1,112 @@
// 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)",
    "zh-tw": "chinese (traditional)",
    "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",
}
-- 
2.20.1
py-googletrans probably uses a different API, this list doesn't work
fully with gtranslate:

[PATCH gtranslate 2/3] Replaced language selector. Export this patch

Kyle Anthony Williams
Replaced <input> elements with <select> and added Languages to indexPageData.
---
 templates/index.html | 10 ++++++++--
 web.go               | 19 ++++++++++---------
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/templates/index.html b/templates/index.html
index 22870fa..c02216c 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -16,14 +16,20 @@
          <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, $lang := .Languages}}<option value="{{$code}}">{{$lang}}</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, $lang := .Languages}}<option value="{{$code}}">{{$lang}}</option>{{end}}
          </select>
          <!-- <input id="to" name="to" class="form__input" placeholder="fr,ru" value="{{.To}}"> -->
        </div>

        <div class="form__field">
diff --git a/web.go b/web.go
index 9b1ff12..3411f1a 100644
--- a/web.go
+++ b/web.go
@@ -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})
	}
}
-- 
2.20.1

[PATCH gtranslate 3/3] Added title casing to language selectors. Export this patch

Kyle Anthony Williams
In order to add a FuncMap to the templating system, I had to make use of template.New. I then used my new function in the index template.
---
 main.go              | 7 ++++++-
 templates/index.html | 4 ++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/main.go b/main.go
index 9fe37f3..8e94c27 100644
--- a/main.go
+++ b/main.go
@@ -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()

diff --git a/templates/index.html b/templates/index.html
index c02216c..c3a0370 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -17,7 +17,7 @@
            Source language:
          </label>
          <select name="from" id="from" class="form__input">
            {{range $code, $lang := .Languages}}<option value="{{$code}}">{{$lang}}</option>{{end}}
            {{range $code, $lang := .Languages}}<option value="{{$code}}">{{$lang|title}}</option>{{end}}
          </select>
          <!-- <input id="from" name="from" class="form__input" placeholder="en,de" value="{{.From}}"> -->
        </div>
@@ -27,7 +27,7 @@
            Translate to:
          </label>
          <select name="to" id="to" class="form__input">
            {{range $code, $lang := .Languages}}<option value="{{$code}}">{{$lang}}</option>{{end}}
            {{range $code, $lang := .Languages}}<option value="{{$code}}">{{$lang|title}}</option>{{end}}
          </select>
          <!-- <input id="to" name="to" class="form__input" placeholder="fr,ru" value="{{.To}}"> -->
        </div>
-- 
2.20.1