1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Do not use shared variable to translate templates

This commit is contained in:
Frédéric Guillot 2018-04-27 22:07:46 -07:00
parent 6b360d08c1
commit ddd3af4b85
21 changed files with 104 additions and 101 deletions

View file

@ -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()

View file

@ -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}
}