Add language to routing as well

This commit is contained in:
Achim Rohn
2025-08-11 18:30:54 +02:00
parent 2f1a18f676
commit 37439305d3
+14 -10
View File
@@ -12,16 +12,17 @@ type HtmxContext interface {
} }
type HtmxContextImpl struct { type HtmxContextImpl struct {
req *http.Request req *http.Request
res http.ResponseWriter res http.ResponseWriter
language Language
} }
func (c *HtmxContextImpl) GetLanguage() Language { func (c *HtmxContextImpl) GetLanguage() Language {
return En return c.language
} }
func NewHtmxContext(req *http.Request, res http.ResponseWriter) HtmxContext { func NewHtmxContext(req *http.Request, res http.ResponseWriter, language Language) HtmxContext {
return &HtmxContextImpl{req: req, res: res} return &HtmxContextImpl{req: req, res: res, language: language}
} }
func (c *HtmxContextImpl) Render(node gomponents.Node) { func (c *HtmxContextImpl) Render(node gomponents.Node) {
@@ -53,16 +54,19 @@ type HtmxGetRoute struct {
Path string Path string
RouteFunc HtmxRouteFunc RouteFunc HtmxRouteFunc
PathParams []HtmxPathParam PathParams []HtmxPathParam
Languages []Language
} }
func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc) *HtmxGetRoute { func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute {
return &HtmxGetRoute{Path: path, RouteFunc: routeFunc} return &HtmxGetRoute{Path: path, RouteFunc: routeFunc, Languages: languages}
} }
func (h HtmxGetRoute) Add(server *http.ServeMux) { func (h HtmxGetRoute) Add(server *http.ServeMux) {
server.HandleFunc("GET "+h.Path, func(res http.ResponseWriter, req *http.Request) { for _, language := range h.Languages {
h.RouteFunc(NewHtmxContext(req, res)) server.HandleFunc("GET /"+string(language)+h.Path, func(res http.ResponseWriter, req *http.Request) {
}) h.RouteFunc(NewHtmxContext(req, res, language))
})
}
} }
func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute { func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute {