Skip to content

Redirection breaks all non-get requests #11

@johannesE

Description

@johannesE

Hi there, I currently have to exclude all requests from the setLocale plug that are not get requests. Is there a way to have them work with this plug?

E.g. my router.ex:

defmodule DatabergWeb.Router do
  use DatabergWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :localeSettings do
    plug SetLocale, gettext: DatabergWeb.Gettext, default_locale: "en", cookie_key: "project_locale"
    plug :setLocaleCookie
  end

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

  scope "/", DatabergWeb do
    pipe_through :browser # Use the default browser stack
    resources "/projects", ProjectController, except: [:index]
  end

  scope "/", DatabergWeb do
    pipe_through :browser # Use the default browser stack
    pipe_through :localeSettings

    get "/", PageController, :index
    resources "/projects", ProjectController, only: [:index]
  end

  scope "/:locale", DatabergWeb do
    pipe_through :browser
    pipe_through :localeSettings

    get "/", PageController, :index
    resources "/projects", ProjectController
  end

  def setLocaleCookie(conn, params) do
    put_resp_cookie conn, "project_locale", conn.assigns.locale, max_age: 10*24*60*60
  end
end

Btw: You might wanna include this cool setLocaleCookie plug in the readme or the code. It makes the language work when a user specifically visits a route other than the default one without having to change all url's.
So when I go to example.rocks, it redirects me to example.rocks/en (default locale). The user goes to example.rocks/ru because he prefers russian, clicks on a link and it will not put him to example.rocks/en/link, but rather example.rocks/ru/link.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions