From e3e0a8520bcbe87e33fb944f8f696ffeb6cd2807 Mon Sep 17 00:00:00 2001 From: Achim Rohn Date: Wed, 13 Aug 2025 10:19:53 +0200 Subject: [PATCH] Add language dependant paths --- htmx_route.go | 15 +++++++-------- i18n.go | 10 ++++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/htmx_route.go b/htmx_route.go index 26aee32..ab08d11 100644 --- a/htmx_route.go +++ b/htmx_route.go @@ -96,10 +96,9 @@ func addLanguageToPath(path string, language Language) string { } type HtmxGetRoute struct { - Path string + Paths LanguagePaths RouteFunc HtmxRouteFunc PathParams []HtmxPathParam - Languages []Language ActivePath *ActivePath } @@ -108,13 +107,13 @@ func (h HtmxGetRoute) SetActivePath(activePath *ActivePath) HtmxRoute { return h } -func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute { - return &HtmxGetRoute{Path: path, RouteFunc: routeFunc, Languages: languages} +func NewHtmxGetRoute(routeFunc HtmxRouteFunc, paths LanguagePaths) *HtmxGetRoute { + return &HtmxGetRoute{RouteFunc: routeFunc, Paths: paths} } func (h HtmxGetRoute) Add(server *http.ServeMux) { - for _, language := range h.Languages { - server.HandleFunc("GET "+addLanguageToPath(h.Path, language), func(res http.ResponseWriter, req *http.Request) { + for language, path := range h.Paths { + server.HandleFunc("GET "+addLanguageToPath(path, language), func(res http.ResponseWriter, req *http.Request) { context := NewHtmxContext(req, res, language) if h.ActivePath != nil { context.SetActivePath(h.ActivePath) @@ -130,10 +129,10 @@ func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute { } func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) string { + path := addLanguageToPath(h.Paths[language], language) if len(h.PathParams) == 0 { - return h.Path + return path } - path := addLanguageToPath(h.Path, language) for _, param := range h.PathParams { path = strings.ReplaceAll(path, "{"+param.Key+"}", param.Value) } diff --git a/i18n.go b/i18n.go index 84abb4b..8775641 100644 --- a/i18n.go +++ b/i18n.go @@ -72,20 +72,22 @@ func (t I18nText) GetKey() string { return "" } +type LanguagePaths map[Language]string + type ActivePath struct { I18nText - Path string + Paths LanguagePaths } -func NewActivePath(path string, langMap map[Language]string) ActivePath { +func NewActivePath(langMap map[Language]string, paths LanguagePaths) ActivePath { return ActivePath{ I18nText: NewI18nText(langMap), - Path: path, + Paths: paths, } } func (a ActivePath) GetPath(language Language) string { - return "/" + string(language) + a.Path + return "/" + string(language) + a.Paths[language] } func (a ActivePath) IsActive(c HtmxContext) bool {