Fix and improve language handling
- Refactor plugs to detect and set language - Translate ecto validation errors - Use Gettext directly, not Mobilizon.Web.Gettext - Set the language in the <html> attribute according to the one loaded on front-end Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -7,42 +7,87 @@ defmodule Mobilizon.Web.Plugs.SetLocalePlugTest do
|
||||
use ExUnit.Case, async: true
|
||||
use Plug.Test
|
||||
|
||||
alias Mobilizon.Web.Gettext, as: GettextBackend
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Web.Plugs.SetLocalePlug
|
||||
alias Plug.Conn
|
||||
|
||||
test "default locale is `en`" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> SetLocalePlug.call([])
|
||||
describe "test assigning locale to conn" do
|
||||
test "use supported locale from `accept-language`" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> assign(:detected_locale, "ru")
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: ""} == conn.assigns
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru", detected_locale: "ru"} == conn.assigns
|
||||
end
|
||||
|
||||
test "use default locale if locale from `accept-language` is not supported" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> Conn.put_req_header("accept-language", "tlh")
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
end
|
||||
end
|
||||
|
||||
test "use supported locale from `accept-language`" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
describe "test getting default locale from instance" do
|
||||
test "default locale is `en`" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale(GettextBackend)
|
||||
assert %{locale: "ru"} == conn.assigns
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
end
|
||||
|
||||
test "with empty string returns the default configured locale" do
|
||||
Config.put([:instance, :default_language], "es")
|
||||
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert %{locale: "es"} == conn.assigns
|
||||
|
||||
Config.put([:instance, :default_language], "en")
|
||||
end
|
||||
|
||||
test "with empty string returns english as a proper fallback if the default configured locale is nil" do
|
||||
Config.put([:instance, :default_language], nil)
|
||||
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
|
||||
Config.put([:instance, :default_language], "en")
|
||||
end
|
||||
end
|
||||
|
||||
test "use default locale if locale from `accept-language` is not supported" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> Conn.put_req_header("accept-language", "tlh")
|
||||
|> SetLocalePlug.call([])
|
||||
describe "test determine_best_locale/1" do
|
||||
test "with empty string returns the default locale" do
|
||||
assert SetLocalePlug.determine_best_locale("") == nil
|
||||
end
|
||||
|
||||
assert "en" == Gettext.get_locale(GettextBackend)
|
||||
assert %{locale: ""} == conn.assigns
|
||||
test "returns fallback with an unexisting locale" do
|
||||
assert SetLocalePlug.determine_best_locale("yolo") == nil
|
||||
end
|
||||
|
||||
test "maps the correct part if the locale has multiple ones" do
|
||||
assert SetLocalePlug.determine_best_locale("fr_CA") == "fr"
|
||||
end
|
||||
|
||||
test "returns the locale if valid" do
|
||||
assert SetLocalePlug.determine_best_locale("es") == "es"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user