Changeset View
Changeset View
Standalone View
Standalone View
lib/kolab_chat/web/controllers/plugs/locale.ex
- This file was moved from web/controllers/plugs/locale.ex.
defmodule KolabChat.Plugs.Locale do | defmodule KolabChat.Plugs.Locale do | ||||
import Plug.Conn | import Plug.Conn | ||||
def init(opts), do: opts | def init(opts), do: opts | ||||
def call(conn, _opts) do | def call(conn, _opts) do | ||||
case get_session(conn, :locale) || client_locale(conn) do | case get_session(conn, :locale) || client_locale(conn) do | ||||
nil -> | nil -> | ||||
conn | conn | ||||
locale -> | locale -> | ||||
Gettext.put_locale(KolabChat.Gettext, locale) | Gettext.put_locale(KolabChat.Web.Gettext, locale) | ||||
put_session(conn, :locale, locale) | put_session(conn, :locale, locale) | ||||
end | end | ||||
end | end | ||||
# Gets supported locale code from the client | # Gets supported locale code from the client | ||||
# Uses 'locale' parameter or Accept-Language header | # Uses 'locale' parameter or Accept-Language header | ||||
defp client_locale(conn) do | defp client_locale(conn) do | ||||
supported = Gettext.known_locales(KolabChat.Gettext) | supported = Gettext.known_locales(KolabChat.Web.Gettext) | ||||
locale = conn.params["locale"] || get_req_header(conn, "accept-language") | locale = conn.params["locale"] || get_req_header(conn, "accept-language") | ||||
locale | locale | ||||
|> to_string() | |> to_string() | ||||
|> String.split(",") | |> String.split(",") | ||||
|> Enum.map(&parse_locale/1) | |> Enum.map(&parse_locale/1) | ||||
|> Enum.filter(fn(x) -> Enum.member?(supported, x) end) | |> Enum.filter(fn(x) -> Enum.member?(supported, x) end) | ||||
|> Enum.at(0) | |> Enum.at(0) | ||||
Show All 11 Lines |