2017-08-04 19:16:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/nicksnyder/go-i18n/i18n"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, filename := range AssetNames() {
|
|
|
|
if !strings.HasPrefix(filename, "frontend/locale") || !strings.HasSuffix(filename, ".all.json") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
translationData, _ := Asset(filename)
|
|
|
|
if err := i18n.ParseTranslationFileBytes(filename, translationData); err != nil {
|
|
|
|
log.Fatalf("Unable to load translation data %q: %s", filename, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 17:08:02 +00:00
|
|
|
func addTranslateFunc(t template.FuncMap, r *http.Request) template.FuncMap {
|
2017-08-04 19:16:00 +00:00
|
|
|
cookie, _ := r.Cookie("lang")
|
|
|
|
|
|
|
|
cookieLang := ""
|
|
|
|
if cookie != nil {
|
|
|
|
cookieLang = cookie.Value
|
|
|
|
}
|
|
|
|
qpLang := r.URL.Query().Get("hl")
|
|
|
|
acceptLang := r.Header.Get("Accept-Language")
|
|
|
|
defaultLang := "en-US" // known valid language
|
|
|
|
|
|
|
|
T, _ := i18n.Tfunc(cookieLang, qpLang, acceptLang, defaultLang)
|
2018-05-05 17:08:02 +00:00
|
|
|
|
|
|
|
t["T"] = T
|
|
|
|
return t
|
2017-08-04 19:16:00 +00:00
|
|
|
}
|