aboutsummaryrefslogtreecommitdiffhomepage
path: root/template
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2018-04-27 22:07:46 -0700
committerGravatar Frédéric Guillot <fred@miniflux.net>2018-04-27 22:07:46 -0700
commitddd3af4b85f5a2fe85c17a9cc30cf54743b9eb59 (patch)
tree539b491fc9f67980b09a6ccd282454614a77f8b4 /template
parent6b360d08c1f6c8a6cd1b7608f7af734a3ceef8d7 (diff)
Do not use shared variable to translate templates
Diffstat (limited to 'template')
-rw-r--r--template/engine.go37
-rw-r--r--template/functions.go32
2 files changed, 39 insertions, 30 deletions
diff --git a/template/engine.go b/template/engine.go
index 7d19c8f..1afeaf9 100644
--- a/template/engine.go
+++ b/template/engine.go
@@ -8,8 +8,10 @@ import (
"bytes"
"html/template"
"io"
+ "time"
"github.com/miniflux/miniflux/config"
+ "github.com/miniflux/miniflux/errors"
"github.com/miniflux/miniflux/locale"
"github.com/miniflux/miniflux/logger"
@@ -35,18 +37,37 @@ func (e *Engine) parseAll() {
}
}
-// SetLanguage change the language for template processing.
-func (e *Engine) SetLanguage(language string) {
- e.funcMap.Language = e.translator.GetLanguage(language)
-}
-
-// Execute process a template.
-func (e *Engine) Execute(w io.Writer, name string, data interface{}) {
+// Render process a template and write the ouput.
+func (e *Engine) Render(w io.Writer, name, language string, data interface{}) {
tpl, ok := e.templates[name]
if !ok {
logger.Fatal("[Template] The template %s does not exists", name)
}
+ lang := e.translator.GetLanguage(language)
+ tpl.Funcs(template.FuncMap{
+ "elapsed": func(timezone string, t time.Time) string {
+ return elapsedTime(lang, timezone, t)
+ },
+ "t": func(key interface{}, args ...interface{}) string {
+ switch key.(type) {
+ case string:
+ return lang.Get(key.(string), args...)
+ case errors.LocalizedError:
+ return key.(errors.LocalizedError).Localize(lang)
+ case *errors.LocalizedError:
+ return key.(*errors.LocalizedError).Localize(lang)
+ case error:
+ return key.(error).Error()
+ default:
+ return ""
+ }
+ },
+ "plural": func(key string, n int, args ...interface{}) string {
+ return lang.Plural(key, n, args...)
+ },
+ })
+
var b bytes.Buffer
err := tpl.ExecuteTemplate(&b, "base", data)
if err != nil {
@@ -61,7 +82,7 @@ func NewEngine(cfg *config.Config, router *mux.Router, translator *locale.Transl
tpl := &Engine{
templates: make(map[string]*template.Template),
translator: translator,
- funcMap: newFuncMap(cfg, router, translator.GetLanguage("en_US")),
+ funcMap: newFuncMap(cfg, router),
}
tpl.parseAll()
diff --git a/template/functions.go b/template/functions.go
index 81ce1db..e80a4a5 100644
--- a/template/functions.go
+++ b/template/functions.go
@@ -12,17 +12,14 @@ import (
"github.com/gorilla/mux"
"github.com/miniflux/miniflux/config"
- "github.com/miniflux/miniflux/errors"
"github.com/miniflux/miniflux/filter"
"github.com/miniflux/miniflux/http/route"
- "github.com/miniflux/miniflux/locale"
"github.com/miniflux/miniflux/url"
)
type funcMap struct {
- cfg *config.Config
- router *mux.Router
- Language *locale.Language
+ cfg *config.Config
+ router *mux.Router
}
func (f *funcMap) Map() template.FuncMap {
@@ -77,30 +74,21 @@ func (f *funcMap) Map() template.FuncMap {
"isodate": func(ts time.Time) string {
return ts.Format("2006-01-02 15:04:05")
},
+ "dict": dict,
+
+ // These functions are overrided at runtime after the parsing.
"elapsed": func(timezone string, t time.Time) string {
- return elapsedTime(f.Language, timezone, t)
+ return ""
},
"t": func(key interface{}, args ...interface{}) string {
- switch key.(type) {
- case string:
- return f.Language.Get(key.(string), args...)
- case errors.LocalizedError:
- return key.(errors.LocalizedError).Localize(f.Language)
- case *errors.LocalizedError:
- return key.(*errors.LocalizedError).Localize(f.Language)
- case error:
- return key.(error).Error()
- default:
- return ""
- }
+ return ""
},
"plural": func(key string, n int, args ...interface{}) string {
- return f.Language.Plural(key, n, args...)
+ return ""
},
- "dict": dict,
}
}
-func newFuncMap(cfg *config.Config, router *mux.Router, language *locale.Language) *funcMap {
- return &funcMap{cfg, router, language}
+func newFuncMap(cfg *config.Config, router *mux.Router) *funcMap {
+ return &funcMap{cfg, router}
}