Add language dependant paths
This commit is contained in:
+7
-8
@@ -96,10 +96,9 @@ func addLanguageToPath(path string, language Language) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type HtmxGetRoute struct {
|
type HtmxGetRoute struct {
|
||||||
Path string
|
Paths LanguagePaths
|
||||||
RouteFunc HtmxRouteFunc
|
RouteFunc HtmxRouteFunc
|
||||||
PathParams []HtmxPathParam
|
PathParams []HtmxPathParam
|
||||||
Languages []Language
|
|
||||||
ActivePath *ActivePath
|
ActivePath *ActivePath
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,13 +107,13 @@ func (h HtmxGetRoute) SetActivePath(activePath *ActivePath) HtmxRoute {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute {
|
func NewHtmxGetRoute(routeFunc HtmxRouteFunc, paths LanguagePaths) *HtmxGetRoute {
|
||||||
return &HtmxGetRoute{Path: path, RouteFunc: routeFunc, Languages: languages}
|
return &HtmxGetRoute{RouteFunc: routeFunc, Paths: paths}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HtmxGetRoute) Add(server *http.ServeMux) {
|
func (h HtmxGetRoute) Add(server *http.ServeMux) {
|
||||||
for _, language := range h.Languages {
|
for language, path := range h.Paths {
|
||||||
server.HandleFunc("GET "+addLanguageToPath(h.Path, language), func(res http.ResponseWriter, req *http.Request) {
|
server.HandleFunc("GET "+addLanguageToPath(path, language), func(res http.ResponseWriter, req *http.Request) {
|
||||||
context := NewHtmxContext(req, res, language)
|
context := NewHtmxContext(req, res, language)
|
||||||
if h.ActivePath != nil {
|
if h.ActivePath != nil {
|
||||||
context.SetActivePath(h.ActivePath)
|
context.SetActivePath(h.ActivePath)
|
||||||
@@ -130,10 +129,10 @@ func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) string {
|
func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) string {
|
||||||
|
path := addLanguageToPath(h.Paths[language], language)
|
||||||
if len(h.PathParams) == 0 {
|
if len(h.PathParams) == 0 {
|
||||||
return h.Path
|
return path
|
||||||
}
|
}
|
||||||
path := addLanguageToPath(h.Path, language)
|
|
||||||
for _, param := range h.PathParams {
|
for _, param := range h.PathParams {
|
||||||
path = strings.ReplaceAll(path, "{"+param.Key+"}", param.Value)
|
path = strings.ReplaceAll(path, "{"+param.Key+"}", param.Value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,20 +72,22 @@ func (t I18nText) GetKey() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LanguagePaths map[Language]string
|
||||||
|
|
||||||
type ActivePath struct {
|
type ActivePath struct {
|
||||||
I18nText
|
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{
|
return ActivePath{
|
||||||
I18nText: NewI18nText(langMap),
|
I18nText: NewI18nText(langMap),
|
||||||
Path: path,
|
Paths: paths,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a ActivePath) GetPath(language Language) string {
|
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 {
|
func (a ActivePath) IsActive(c HtmxContext) bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user