defmodule WhoNeedHelpWeb.Locale do
  import Plug.Conn

  @locales ~w(en uk ru)

  def init(opts), do: opts

  def call(conn, _opts) do
    locale =
      conn.params["locale"] ||
        get_session(conn, :locale) ||
        get_in(conn.assigns, [:current_scope, Access.key(:user), Access.key(:locale)]) ||
        "en"

    locale = if locale in @locales, do: locale, else: "en"
    Gettext.put_locale(WhoNeedHelpWeb.Gettext, locale)
    put_session(conn, :locale, locale)
  end
end
