Language switcher and link generation

Hello,

I will start with i am new to Phoenix and Elixir so please consider it before reading my question.

I am trying to get multi-lingual site. So far i have the gettext files and Phoenix configuration work properly. At the current stage i want to implement the language switcher. For example:

  1. User opens mysite.com/en/ping
  2. User need to have the UI on another language and clicks on the Bulgarian flag of from the language switch on top
  3. User gets redirected to /bg/ping

My question is - is there a smart way to generate a link for the same page the user is currently at or if there is another better approach for such situation?

My goal is when a person opens a long url like mysite.com/en/param1/param2/group/id but the UI is not conveniently matching their language to be able to switch it and stay on the same page.

I hope this makes sense. Any help will be appreciated.

Regards,
Peter

Probably it is not the best way to do it but i managed to make it work with the following code:

<%= for langs <- Gettext.known_locales(DemoWeb.Gettext) do %>

<a href="<%= @conn.scheme %>://<%= @conn.host %>:<%= @conn.port %><%= String.replace(@conn.request_path, ~r/(?<=^.{1})../, langs) %>" > <%= langs %></a>

<%= end %>

It takes the configured locales and “iterates” trough the list of the locales and replacing the 2nd and 4rd characters for every locale for the current URL.