Takueg

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

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

BartOtten

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

Where Next?

Popular in Questions Top

beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement