diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-08-07 01:45:48 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-08-07 01:45:48 +0200 |
commit | e555accb612e189ab9bdad403958d4e0a6d809ae (patch) | |
tree | 319c4937a165a4b867a42f1852c3362ba577c68d | |
parent | 7fbaed9158ac5c405ee3150c2bf27af8ec1380ba (diff) |
Redirect the user after a language is set
-rw-r--r-- | main.go | 12 | ||||
-rw-r--r-- | middleware/i18n.go | 9 |
2 files changed, 17 insertions, 4 deletions
@@ -1,7 +1,7 @@ package main import ( - "context" + "errors" "flag" "fmt" "log" @@ -69,4 +69,14 @@ func setUserLanguage(w http.ResponseWriter, r *http.Request) { Value: loc, MaxAge: math.MaxInt32, }) + + if c, err := r.Cookie("redirect"); errors.Is(err, http.ErrNoCookie) { + http.Redirect(w, r, "/", http.StatusFound) + } else { + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + MaxAge: -1, + }) + http.Redirect(w, r, c.Value, http.StatusFound) + } } diff --git a/middleware/i18n.go b/middleware/i18n.go index 5e33636..a30a9f6 100644 --- a/middleware/i18n.go +++ b/middleware/i18n.go @@ -28,12 +28,15 @@ func I18n(next http.Handler) http.Handler { } } - used := cmp.Or(p, i18n.DefaultPrinter) - ctx := context.WithValue(r.Context(), PrinterKey, used) + ctx := context.WithValue( + r.Context(), PrinterKey, cmp.Or(p, i18n.DefaultPrinter)) if p == pZero { + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + Value: r.URL.Path, + }) templates.Root(nil, templates.SetLanguage()).Render(ctx, w) - /* TODO: Redirect the user back to where they came from */ } else { next.ServeHTTP(w, r.WithContext(ctx)) } |