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.

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 :slight_smile:

1 Like

Something I have seen done in the past is the following in your router.

@locales ["en", "de"]

for locale <- @locales do
  scope "/#{locale}" do
    # The rest of your routes go here.

I will admit I am unsure if this is the suggested practice or if there is something better that could be done.

When my newborn is nice and quiet, I will pick this one up. Mostly missing documentation I guess.

Spend some time tonight to update the code for use with the latest LiveView. Would you like access to the fully functioning alpha version or can you wait a week longer?

@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.


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.


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

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.