@Takueg I have worked at the lib today and also wrote the initial README. Does this sound like something that could solve the first issue of alternative routes for you? If so, I will publish asap.
ps. It won’t do the actual redirect, but I know from a Proof of Concept that it can be done by leveraging some helpers provided by the lib.
Phx.AltRoutes
Generate alternative routes and route helpers in Phoenix applications.
Bonus features:
- Each alternative route can have custom assigns.
- When you define a
Gettext
module in the configuration, Gettext extracts the route segments into route.po files when invoking mix gettext.extract
; allowing the translation of routes along with other Gettext
messages.
Example
The examples below will use the following configuration.
defmodule ExampleWeb.AltRoutes do
use Phx.AltRoutes,
routes: %{
"europe" => %{
short: "eu",
slug: "europe",
assigns: %{language: "en", locale: "en"},
routes: %{
"netherlands" => %{
slug: "nl",
assigns: %{language: "nl", locale: "nl"}
},
"belgium" => %{
slug: "be",
assigns: %{language: "nl", locale: "be"}
}
}
},
"united-kingdom" => %{
slug: "uk",
assigns: %{language: "en", locale: "en"}
}
},
gettext_module: ExampleWeb.Gettext
end
Translated routes
Phoenix will generate routes.po
files for languages ‘nl’ and ‘en’, including all segments of nested routes. The macro expands the routes during compile time with alternative variants.
Generated routes
Example of the routes to edit products.
# original
product_index_path /products/:id/edit
# generated
eu_product_index_path /europe/products/:id/edit
eu_be_product_index_path /europe/be/producten/:id/bewerken
eu_nl_product_index_path /europe/nl/producten/:id/bewerken
uk_product_index_path /uk/products/:id/edit
Links in templates
During the rendering of templates, the library will replace links of routes with alternatives. This:
<%= link "Log in", to: Routes.user_session_path(@conn, :new) %></li>
will render a link to an alternative route based on the current alternative scope.
- page
/uk/products
links to /uk/users/log_in
- page
/europe/nl/producten
links to /europe/nl/gebruikers/aanmelden
Use of custom assigns per route
As per configuration you can use the custom assigns @locale
and @language
in Phoenix templates.