Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirection breaks all non-get requests #11

Open
johannesE opened this issue Apr 27, 2018 · 1 comment
Open

Redirection breaks all non-get requests #11

johannesE opened this issue Apr 27, 2018 · 1 comment

Comments

@johannesE
Copy link

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.

@Hermanverschooten
Copy link

You need not duplicate the routes, but make sure you pass the locale into the routes helper, that fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants