From 3cb7572a8f1092b3169f4faaa2e76776dea39993 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Wed, 7 Aug 2024 01:51:13 +0200 Subject: Just use hardcoded strings --- main.go | 6 +++--- middleware/i18n.go | 6 ++---- templates/base.go | 5 ----- templates/navbar.templ | 2 +- templates/navbar_templ.go | 2 +- templates/root.templ | 8 ++++---- templates/root_templ.go | 43 +++++++++++++++---------------------------- 7 files changed, 26 insertions(+), 46 deletions(-) diff --git a/main.go b/main.go index 6ecdf32..e0c88ef 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main() { } func finalHandler(w http.ResponseWriter, r *http.Request) { - p := r.Context().Value(middleware.PrinterKey).(i18n.Printer) + p := r.Context().Value("printer").(i18n.Printer) /* Strip trailing slash from the URL */ path := r.URL.Path @@ -57,7 +57,7 @@ func finalHandler(w http.ResponseWriter, r *http.Request) { } func setUserLanguage(w http.ResponseWriter, r *http.Request) { - loc := r.FormValue(templates.LocaleKey) + loc := r.FormValue("locale") _, ok := i18n.Printers[strings.ToLower(loc)] if !ok { w.WriteHeader(http.StatusBadRequest) @@ -65,7 +65,7 @@ func setUserLanguage(w http.ResponseWriter, r *http.Request) { return } http.SetCookie(w, &http.Cookie{ - Name: "lang", + Name: "locale", Value: loc, MaxAge: math.MaxInt32, }) diff --git a/middleware/i18n.go b/middleware/i18n.go index a30a9f6..0ed07d4 100644 --- a/middleware/i18n.go +++ b/middleware/i18n.go @@ -12,13 +12,11 @@ import ( "git.thomasvoss.com/euro-cash.eu/templates" ) -const PrinterKey = "printer" - func I18n(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var p, pZero i18n.Printer - if c, err := r.Cookie("lang"); errors.Is(err, http.ErrNoCookie) { + if c, err := r.Cookie("locale"); errors.Is(err, http.ErrNoCookie) { log.Println("Language cookie not set") } else { var ok bool @@ -29,7 +27,7 @@ func I18n(next http.Handler) http.Handler { } ctx := context.WithValue( - r.Context(), PrinterKey, cmp.Or(p, i18n.DefaultPrinter)) + r.Context(), "printer", cmp.Or(p, i18n.DefaultPrinter)) if p == pZero { http.SetCookie(w, &http.Cookie{ diff --git a/templates/base.go b/templates/base.go index f0b288b..e6647f2 100644 --- a/templates/base.go +++ b/templates/base.go @@ -1,8 +1,3 @@ package templates -const ( - PrinterKey = "printer" - LocaleKey = "locale" -) - //go:generate templ generate -log-level warn diff --git a/templates/navbar.templ b/templates/navbar.templ index 95e8563..5007183 100644 --- a/templates/navbar.templ +++ b/templates/navbar.templ @@ -3,7 +3,7 @@ package templates import "git.thomasvoss.com/euro-cash.eu/i18n" templ navbar() { - {{ p := ctx.Value(PrinterKey).(i18n.Printer) }} + {{ p := ctx.Value("printer").(i18n.Printer) }}