Go

Go Code

This way to call a function from a Go template can be used for many purposes. This is only one tiny example how it works. It may be possible to calculate values etc.

1. Tell the templates to use the function "lang"

translations = make(map[string]*gotext.Po)
languages := []string{"en", "fr", "de", "es", "it"}
for _, lang := range languages {
	path := "./public/locale/" + lang + ".po"
	lang_po := gotext.NewPo()
	lang_po.ParseFile(path)
	translations[lang] = lang_po
}

tpl = template.Must(template.New("").Funcs(template.FuncMap{
	"trans": trans,
}).ParseGlob("./public/tmpl/*/*.html"))

tpl = template.Must(tpl.New("").Funcs(template.FuncMap{
	"trans": trans,
}).ParseGlob("./public/tmpl/*/*/*.html"))

2. Create the function "trans"

func trans(key string) string {
    path := "./public/locale/" + usr_lang + ".po"
    po, ok := translations[usr_lang]
    if !ok {
        po = gotext.NewPo()
        po.ParseFile(path)
        translations[usr_lang] = po
    }
    return po.Get(key)
}            

3. All Go code put together and fetch the stored language from a cookie

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "html/template"
    "net/http"
    "strings"
                
    "github.com/leonelquinteros/gotext"
)
                
var tpl *template.Template
var translations map[string]*gotext.Po
var usr_lang string
                
func init() {
    translations = make(map[string]*gotext.Po)
    languages := []string{"en", "fr", "de", "es", "it"}
    for _, lang := range languages {
        path := "./public/locale/" + lang + ".po"
        lang_po := gotext.NewPo()
        lang_po.ParseFile(path)
        translations[lang] = lang_po
    }
    
    //level 1 folders
    tpl = template.Must(template.New("").Funcs(template.FuncMap{
        "trans": trans,
    }).ParseGlob("./public/tmpl/*/*.html"))
    
    //level 2 folders
    tpl = template.Must(tpl.New("").Funcs(template.FuncMap{
        "trans": trans,
    }).ParseGlob("./public/tmpl/*/*/*.html"))
                
    http.Handle("/img/", http.StripPrefix("/img/", http.FileServer(http.Dir("./public/img"))))
    http.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir("./public/css"))))
    http.Handle("/icn/", http.StripPrefix("/icn/", http.FileServer(http.Dir("./public/icn"))))
    http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./public/js"))))
    http.Handle("/misc/", http.StripPrefix("/misc/", http.FileServer(http.Dir("./public/misc"))))
}
                
func main() {
    http.HandleFunc("/", endpoint)
    http.ListenAndServe(":9098", nil)
}

//template function
func trans(key string) string {
    path := "./public/locale/" + usr_lang + ".po"
    po, ok := translations[usr_lang]
    if !ok {
        po = gotext.NewPo()
        po.ParseFile(path)
        translations[usr_lang] = po
    }
    return po.Get(key)
}
                
func endpoint(w http.ResponseWriter, r *http.Request) {
    module, mode, val := getpath(r.URL.Path)
    usr_getlang(r)
                
    var page string
    switch module {
    case "robots.txt":
        http.ServeFile(w, r, "public/misc/robots.txt")
    case "sitemap.xml":
        http.ServeFile(w, r, "public/misc/sitemap.xml")
    case "favicon.ico", "favicon-32x32.png", "favicon-16x16.png":
        return
    case "":
        module = "home"
    }
                
    page = module + ".html"
    set_header(w)
    tpl.ExecuteTemplate(w, page, nil)

}
                
func set_header(w http.ResponseWriter) {
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "*")
}
                
// split url
func getpath(path string) (module, mode, val string) {
    parts := strings.Split(path, "/")
    switch len(parts) {
    case 4:
        val = parts[3]
        fallthrough
    case 3:
        mode = parts[2]
        fallthrough
    case 2:
        module = parts[1]
    }
        return // Named return values are used, so just return here
}
                
func usr_getlang(r *http.Request) string {
    lang, _ := r.Cookie("lang")
    if lang != nil {
        usr_lang = lang.Value
    } else {
        usr_lang = "en"
    }
    return usr_lang
}