[routing] how to handle var as first parameter in URI



I’m building a new multilingual website with echo. The URL pattern of each resource is:


For example:

Apparently we can not use parameter as first part of an URI:
e.GET("/:lang/m/dashboard", controllers.Dashboard) -> 404 for /fr/m/dashboard | /en/m/dashboard

Wildcard does not work either, because:
/*/foo/bar matches anything

So is there a better way to handle this problem than copying routes for each language:
e.GET("/fr/m/dashboard", controllers.Dashboard)
e.GET("/en/m/dashboard", controllers.Dashboard)

Note that the aims is not to get “lang” as parameter (a middleware extracts lang from URI and load translation) but not to have to copy route definition for each language.



You can iterate over a slice of languages and register a router?


Thank you @vishr for your suggestion, but my main concern is not really to have to copy myself route definitions for each language (even if as programmer i avoid boring task so if i’ll not find another way, i’ll probably go with your tip) but to find a way to not have to copy routes.

For multilingual site the route pattern “/LANG/foo/bar” is very common, copying routes is, for me, not an elegant, nor idiomatic, way to handle this concern.

There must be a reason which explains why we can’t use a param as first part of an URI, (/:lang/foo/bar) for now i do not find it, i’ll check the code to find the answer :wink:


Ok i have found the problem:

I had the 404 error on /en/foo/bar because i had tested with the route definition ‘/:lang/foo/bar’ while I had this static route /en/xxx/yyyy

My bad :frowning: