Takueg

Takueg

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.

Showing Posts 1 to 6

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:

Ankhers

Ankhers

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

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

BartOtten

BartOtten

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

BartOtten

BartOtten

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?

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews