Default and optional params for route definitions and route helpers

When defining routes I’d like the option to use “smart” route helpers, as in, they can infer the path param value.

Let’s say this is the router configuration:

scope "/:locale" do
  live "/home", HomeLive, :show
  live "/contact", ContactLive, :show
end

If I’m currently on the page /en/contact I want to use the helper home_path(@socket, :show) without specifying the :locale param. We can assume I can access that value somewhere, like having it in socket.assigns.locale.
Same goes for e.g. /de/contact, I want router_path(@socket, :show) to generate the path /de/home.

Is something like that even possible currently?

2 Likes

Option 1: https://elixirforum.com/t/phoenix-localized-routes-localized-multilingual-routes-in-phoenix/

Option 2: cldr_routes/mix at main · elixir-cldr/cldr_routes · GitHub

I’m not silent at all; even published a Release Candidate with support for Verified Routes. I actually wrote code for the lib today. It’s very much maintained and supported.

2 Likes

Thank you. This looks like it solves the issue. I’ll definitely give it a try.

Don’t forget to switch to the 1.7 branch - the other one is for 1.6

If it’s for a new project I advice to use Phoenix 1.7 and verified routes. The RC of Localized Routes has support for those.

Keep in mind the routing is still a bit in flux at Phoenix side. Between 1.7 RC1 and RC2 they reverted it to 1.6 logic. So even the RC of Localized Routes may not work with Phoenix’ latest RC.

1 Like

As a side note, heex syntax would be…

lang={Gettext.get_locale()}
1 Like