ppetrov
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:
- User opens mysite.com/en/ping
- User need to have the UI on another language and clicks on the Bulgarian flag of from the language switch on top
- 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
First Post!
ppetrov
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.
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








