Takueg
Defining 2 route paths for [en, de] locales in 2 languages for the same action using SetLocale plug
Hi everyone. Does anyone know how I can define 2 routes in two different languages for the same action? For example /en/sign-up and /de/ammeldung should both lead to the same action. The app uses SetLocale plug so the routes are defined like this /:locale/signup. when I define a route specifying the locale like /de/ammeldung I get a ERR_TOO_MANY_REDIRECTS error in the browser.
Is it possible to implement some logic somewhere that says when locale is de use /de/ammeldung and when it is en use /en/sign-up. Or does anyone know any solution to this problem.
Most Liked
BartOtten
@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
Gettextmodule in the configuration, Gettext extracts the route segments into route.po files when invokingmix gettext.extract; allowing the translation of routes along with otherGettextmessages.
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/productslinks to/uk/users/log_in - page
/europe/nl/productenlinks 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.
BartOtten
I did create such thing for LiveView but never finalized it. Send me a private message as a reminder and I will pick up where I left and publish ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









