aboutsummaryrefslogtreecommitdiff
path: root/cmd/site/html.go
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-12-01 18:19:32 -0800
committerChristine Dodrill <me@christine.website>2018-12-01 18:19:32 -0800
commit6feb95eb68e895eeb4d49bc695ad347fed0ed304 (patch)
tree36c9b2e6292df08c69e20dec4636dc96969401a9 /cmd/site/html.go
parentdf7258c5c71e294331b962990ef8da8a6ac89494 (diff)
downloadxesite-i18n-2.tar.xz
xesite-i18n-2.zip
try 2i18n-2
Diffstat (limited to 'cmd/site/html.go')
-rw-r--r--cmd/site/html.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/cmd/site/html.go b/cmd/site/html.go
index ba304c5..d85e817 100644
--- a/cmd/site/html.go
+++ b/cmd/site/html.go
@@ -7,6 +7,7 @@ import (
"net/http"
"time"
+ "github.com/Unknwon/i18n"
"github.com/Xe/ln"
)
@@ -21,7 +22,32 @@ func (s *Site) renderTemplatePage(templateFname string, data interface{}) http.H
s.tlock.RLock()
defer s.tlock.RUnlock()
- var t *template.Template
+ const fallbackLang = `en-US`
+
+ getTranslation := func(group, key string, vals ...interface{}) string {
+ var lang string
+ locale, err := GetPreferredLocale(r)
+ if err != nil {
+ ln.Error(r.Context(), err)
+ lang = fallbackLang
+ goto skip
+ }
+
+ if !i18n.IsExist(locale.Lang) {
+ lang = fallbackLang
+ goto skip
+ }
+
+ lang = locale.Lang
+ skip:
+ return i18n.Tr(lang, group+"."+key, vals...)
+ }
+
+ funcMap := template.FuncMap{
+ "trans": getTranslation,
+ }
+
+ var t = template.New(templateFname).Funcs(funcMap)
var err error
if s.templates[templateFname] == nil {