Kin

Kin

Redirect on a default language?

How do I make my default language Hungarian not being present in the URL? Only /en/rolunk but /rolunk when it’s in Hungarian (by default). I guess in router.ex file I have to add some condition in scope “/” part. But I have no idea how. How to do that?

I would like to redirect all /hu/* stuff to /* as well.

But /en/* to stay as /en/*.

Right now my router files looks like this:

defmodule HelloWeb.Router do
  use HelloWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_live_flash
    plug :put_root_layout, html: {HelloWeb.Layouts, :root}
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :home
  end

  # Other scopes may use custom stacks.
  # scope "/api", HelloWeb do
  #   pipe_through :api
  # end

  # Enable LiveDashboard and Swoosh mailbox preview in development
  if Application.compile_env(:hello, :dev_routes) do
    # If you want to use the LiveDashboard in production, you should put
    # it behind authentication and allow only admins to access it.
    # If your application does not have an admins-only section yet,
    # you can use Plug.BasicAuth to set up some basic authentication
    # as long as you are also using SSL (which you should anyway).
    import Phoenix.LiveDashboard.Router

    scope "/dev" do
      pipe_through :browser

      live_dashboard "/dashboard", metrics: HelloWeb.Telemetry
      forward "/mailbox", Plug.Swoosh.MailboxPreview
    end
  end
end

Most Liked

zaljir

zaljir

For others still searching… A simple possible solution is something like this:

  scope "/en", MyWeb do
    pipe_through :browser

    live_session :set_locale_en, on_mount: {MyWeb.SetLocale, :en} do
      live "/", DocumentLive, :home
      live "/:parent_slug/:slug", DocumentLive, :document
      live "/:slug", DocumentLive, :document
    end
  end

  scope "/", MyWeb do
    pipe_through :browser

    live_session :set_locale_nl, on_mount: {MyWeb.SetLocale, :nl} do
      live "/nl/*rest", DocumentLive, :redirect
      live "/", DocumentLive, :home
      live "/:parent_slug/:slug", DocumentLive, :document
      live "/:slug", DocumentLive, :document
    end
  end

Just define a scope for all languages and for the main language redirect to the URL without locale prefix.
MyWeb.SetLocale just assigns the locale as variable to the live view.

This can be simplified even further with macros for example, but you get the idea.

gdub01

gdub01

I like zaljir’s approach! I also just finished doing something like this and used Kip’s ex_cldr_plugs | Hex and ex_cldr_routes | Hex.

I wanted to try to get the user’s accept language header first and default to cldr’s default if not found.
Then I wanted to force all users to have a locale in the route.. then only respect the route’s locale after that.

Router looks like:

  scope "/", MyAppWeb do
    pipe_through :browser

    get "/", PageController, :redirect_to_default_lang

    localize do
      get "/#{locale}", PageController, :home

      live "/#{locale}/videos", VideoLive.Index, :index
      live "/#{locale}/videos/new", VideoLive.Index, :new
      live "/#{locale}/videos/:id/edit", VideoLive.Index, :edit
      live "/#{locale}/videos/:id", VideoLive.Show, :show
      live "/#{locale}/videos/:id/show/edit", VideoLive.Show, :edit
    end

with this in the pipeline:

    plug Cldr.Plug.PutLocale,
      apps: [:cldr, :gettext],
      from: [:route, :query, :path, :accept_language, :session],
      gettext: MyAppWeb.Gettext,
      cldr: MyApp.Cldr

So the plug finds the best language for the user based on route/accept language header, etc.
If the user visits the main page, it hits this redirect controller to get the best language and sends them off to the correct route ie. “/fr”. All navigation links after that expect a locale.

defmodule MyAppWeb.PageController do
  use MyAppWeb, :controller

  def redirect_to_default_lang(conn, _params) do
    redirect(conn, to: "/#{MyApp.Cldr.get_locale().cldr_locale_name}")
  end
end

and this puts the locale in the session:

defmodule MyAppWeb.RestoreLocale do
  def on_mount(:default, _, %{"cldr_locale" => cldr_locale}, socket) do
    MyApp.Cldr.put_locale(cldr_locale)

    Gettext.put_locale(
      MyAppWeb.Gettext,
      Atom.to_string(MyApp.Cldr.get_locale().cldr_locale_name)
    )

    {:cont, socket}
  end

  def on_mount(:default, _params, _session, socket), do: {:cont, socket}
end
abat

abat

Kudos to your approach of handling the situation!

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
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

We're in Beta

About us Mission Statement