Use correct default language when no Accept-Language is set

Closes #792

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-07-21 15:46:04 +02:00
parent cc33ee7ada
commit ae25cba97a
8 changed files with 92 additions and 20 deletions

View File

@@ -9,19 +9,20 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
Plug to set locale for Gettext
"""
import Plug.Conn, only: [get_req_header: 2, assign: 3]
alias Mobilizon.Web.Gettext, as: GettextBackend
def init(_), do: nil
def call(conn, _) do
locale = get_locale_from_header(conn) || Gettext.get_locale()
Gettext.put_locale(locale)
locale = get_locale_from_header(conn)
GettextBackend.put_locale(locale)
assign(conn, :locale, locale)
end
defp get_locale_from_header(conn) do
conn
|> extract_accept_language()
|> Enum.find(&supported_locale?/1)
|> Enum.find("", &supported_locale?/1)
end
defp extract_accept_language(conn) do
@@ -41,7 +42,7 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlug do
end
defp supported_locale?(locale) do
Mobilizon.Web.Gettext
GettextBackend
|> Gettext.known_locales()
|> Enum.member?(locale)
end