diff --git a/lib/kolab_chat/locale.ex b/web/controllers/plugs/locale.ex similarity index 96% rename from lib/kolab_chat/locale.ex rename to web/controllers/plugs/locale.ex index 3a5776e..2bc795c 100644 --- a/lib/kolab_chat/locale.ex +++ b/web/controllers/plugs/locale.ex @@ -1,38 +1,38 @@ -defmodule KolabChat.Locale do +defmodule KolabChat.Plugs.Locale do import Plug.Conn def init(opts), do: opts def call(conn, _opts) do case get_session(conn, :locale) || client_locale(conn) do nil -> conn locale -> Gettext.put_locale(KolabChat.Gettext, locale) put_session(conn, :locale, locale) end end # Gets supported locale code from the client # Uses 'locale' parameter or Accept-Language header defp client_locale(conn) do supported = Gettext.known_locales(KolabChat.Gettext) locale = conn.params["locale"] || get_req_header(conn, "accept-language") locale |> to_string() |> String.split(",") |> Enum.map(&parse_locale/1) |> Enum.filter(fn(x) -> Enum.member?(supported, x) end) |> Enum.at(0) end # Extracts locale code from an element of Accept-Language header defp parse_locale(locale) do locale |> String.split(";") |> Enum.at(0) |> String.trim() |> String.replace("-", "_") end end diff --git a/web/router.ex b/web/router.ex index d6a79e4..eba53cb 100644 --- a/web/router.ex +++ b/web/router.ex @@ -1,35 +1,35 @@ defmodule KolabChat.Router do use KolabChat.Web, :router pipeline :browser do plug :accepts, ["html"] plug :fetch_session plug :fetch_flash plug :protect_from_forgery plug :put_secure_browser_headers - plug KolabChat.Locale + plug KolabChat.Plugs.Locale plug KolabChat.Plugs.SetUser end pipeline :api do plug :accepts, ["json"] end scope "/", KolabChat do pipe_through :browser # Use the default browser stack get "/", PageController, :index end scope "/auth", KolabChat do pipe_through :browser post "/default/callback", AuthController, :default_callback get "/logout", AuthController, :logout end # Other scopes may use custom stacks. # scope "/api", KolabChat do # pipe_through :api # end end