You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use Phoenix.VerifiedRoutes currently supports path_prefixes: [{Gettext, :get_locale, []}] as an option. This is quite limiting though, because it's a static prefix set per module (per use call). ~p"/somewhere" implicitly gets the prefix applied based on context alone.
Often prefixes also happen to be mixed in with non prefixed routes in templates (e.g. in the app layout) and the need for distinct modules makes this confusing.
I'm proposing changing the option to path_prefixes: %{"locale" => {Gettext, :get_locale, []}}, which makes applying the prefix depend on the route provided like so: ~p"/:locale/somewhere". This clearly indicates the use of a prefix, while allowing for multiple prefixes as well as no prefix to coexist.
I'm not sure you'd want to use or write ~p"/:locale/somewhere for every URL that needs to have the locale prefixed.
For example, for all mix phx.gen.auth generated routes, I'd love to simply change the scope from scope "/" do to scope "/:locale" do so the full route would be /:locale/users/register.
I would continue to use ~p"/users/register". I'd expect the configured verified routes configuration like so path_prefixes: %{"locale" => {Gettext, :get_locale, []}} to be smart enough to replace the /:locale from the scope automatically and generate /en/users/register for example. The advantage for all generated files, for example for mix phx.gen.auth is that there is no need to change all the verified routes ~p calls and thus igniter would work for upgrades out of the box as there was no changes of urls.
Another idea would be to give an mfa (module/function/args) configuration to the verified routes macro so they can be pre-processed by the user so it implements its own logic (URL translation, URL rewriting, URL prefixing, etc.). What do you think ?
Right, perhaps this should be called custom_segments, where you can define a segment that gets injected if you write it, but I think it is a separate feature to path prefix.
Meanwhile, I suggest following the steps in the docs, and use Phoenix.VerifiedRoutes without path prefixes in the few places you don't always have localization.
@josevalim the issue mostly arises when path_prefixes is set, it will prefix everything, including static path assets making the layout have errors by default.
Initially I was searching for a guide or documentation that shows how to use the feature. I wasn't able to figure out how to use it on a vanilla phoenix generated project without having to change all routes, including static paths in the layout (which doesn't have a lot of sense to have statics prefixed with a locale). I stumbled on this configuration option because I was searching for an easy path prefix locale configuration while cldr_locale and routex seem overkill. It is currently described as a locale-in-path solution in the docs that's why I tried it out to see if it could solve my problem :).
Activity
LostKobrakai commentedon Oct 1, 2025
@josevalim ping as requested :)
achedeuzot commentedon Oct 3, 2025
I'm not sure you'd want to use or write
~p"/:locale/somewherefor every URL that needs to have the locale prefixed.For example, for all
mix phx.gen.authgenerated routes, I'd love to simply change the scope fromscope "/" dotoscope "/:locale" doso the full route would be/:locale/users/register.So for given configuration like so:
I would continue to use
~p"/users/register". I'd expect the configured verified routes configuration like sopath_prefixes: %{"locale" => {Gettext, :get_locale, []}}to be smart enough to replace the/:localefrom the scope automatically and generate/en/users/registerfor example. The advantage for all generated files, for example formix phx.gen.authis that there is no need to change all the verified routes~pcalls and thus igniter would work for upgrades out of the box as there was no changes of urls.Another idea would be to give an mfa (module/function/args) configuration to the verified routes macro so they can be pre-processed by the user so it implements its own logic (URL translation, URL rewriting, URL prefixing, etc.). What do you think ?
josevalim commentedon Oct 4, 2025
Right, perhaps this should be called
custom_segments, where you can define a segment that gets injected if you write it, but I think it is a separate feature to path prefix.Meanwhile, I suggest following the steps in the docs, and use Phoenix.VerifiedRoutes without path prefixes in the few places you don't always have localization.
achedeuzot commentedon Oct 4, 2025
@josevalim the issue mostly arises when
path_prefixesis set, it will prefix everything, including static path assets making the layout have errors by default.Initially I was searching for a guide or documentation that shows how to use the feature. I wasn't able to figure out how to use it on a vanilla phoenix generated project without having to change all routes, including static paths in the layout (which doesn't have a lot of sense to have statics prefixed with a locale). I stumbled on this configuration option because I was searching for an easy path prefix locale configuration while cldr_locale and routex seem overkill. It is currently described as a locale-in-path solution in the docs that's why I tried it out to see if it could solve my problem :).
josevalim commentedon Oct 4, 2025
It should definitely not apply to assets. I will fix that.
josevalim commentedon Oct 4, 2025
Fixed in main. :)
achedeuzot commentedon Oct 4, 2025
@josevalim amazing 😍