From ddd3af4b85f5a2fe85c17a9cc30cf54743b9eb59 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Fri, 27 Apr 2018 22:07:46 -0700 Subject: Do not use shared variable to translate templates --- http/handler/context.go | 6 +++++- http/handler/handler.go | 7 ------- http/handler/html_response.go | 4 ++-- 3 files changed, 7 insertions(+), 10 deletions(-) (limited to 'http') diff --git a/http/handler/context.go b/http/handler/context.go index 119a4d5..2d70271 100644 --- a/http/handler/context.go +++ b/http/handler/context.go @@ -85,7 +85,11 @@ func (c *Context) UserLanguage() string { return user.Language } - return c.getContextStringValue(middleware.UserLanguageContextKey) + language := c.getContextStringValue(middleware.UserLanguageContextKey) + if language == "" { + language = "en_US" + } + return language } // Translate translates a message in the current language. diff --git a/http/handler/handler.go b/http/handler/handler.go index b88a885..59e8acd 100644 --- a/http/handler/handler.go +++ b/http/handler/handler.go @@ -41,13 +41,6 @@ func (h *Handler) Use(f ControllerFunc) http.Handler { ctx := NewContext(r, h.store, h.router, h.translator) request := NewRequest(r) response := NewResponse(h.cfg, w, r, h.template) - language := ctx.UserLanguage() - - if language != "" { - h.template.SetLanguage(language) - } else { - h.template.SetLanguage("en_US") - } f(ctx, request, response) }) diff --git a/http/handler/html_response.go b/http/handler/html_response.go index 26e5270..4d0dbe5 100644 --- a/http/handler/html_response.go +++ b/http/handler/html_response.go @@ -19,9 +19,9 @@ type HTMLResponse struct { } // Render execute a template and send to the client the generated HTML. -func (h *HTMLResponse) Render(template string, args map[string]interface{}) { +func (h *HTMLResponse) Render(template, language string, args map[string]interface{}) { h.writer.Header().Set("Content-Type", "text/html; charset=utf-8") - h.template.Execute(h.writer, template, args) + h.template.Render(h.writer, template, language, args) } // ServerError sends a 500 error to the browser. -- cgit v1.2.3