Files
summercms/surf/locale_from_principal.go
2026-09-22 13:39:25 +02:00

20 lines
570 B
Go

package surf
import (
"net/http"
"git.golem15.com/golem15/summercms/bouncer"
"git.golem15.com/golem15/summercms/towel"
)
// LocaleFromPrincipal overrides the header locale with the authenticated
// principal's PreferredLocale when that value is non-empty.
func LocaleFromPrincipal(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p, ok := bouncer.User(r.Context()); ok && p.PreferredLocale != "" {
r = r.WithContext(towel.WithLocale(r.Context(), p.PreferredLocale))
}
next.ServeHTTP(w, r)
})
}