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:
Thomas Citharel
2021-07-27 19:47:54 +02:00
parent 7c943dc09a
commit a670a7d7a7
37 changed files with 286 additions and 218 deletions

View File

@@ -374,7 +374,7 @@ defmodule Mobilizon.Web.Resolvers.EventTest do
)
assert hd(res["errors"])["message"] == %{
"maximum_attendee_capacity" => ["must be greater than or equal to %{number}"]
"maximum_attendee_capacity" => ["must be greater than or equal to 0"]
}
end

View File

@@ -308,6 +308,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
test "create_user/3 doesn't allow two users with the same email", %{conn: conn} do
res =
conn
|> put_req_header("accept-language", "fr")
|> AbsintheHelpers.graphql_query(
query: @create_user_mutation,
variables: @user_creation
@@ -397,6 +398,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
test "register_person/3 doesn't register a profile from an unknown email", %{conn: conn} do
conn
|> put_req_header("accept-language", "fr")
|> AbsintheHelpers.graphql_query(
query: @create_user_mutation,
variables: @user_creation
@@ -416,6 +418,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
test "register_person/3 can't be called with an existing profile", %{conn: conn} do
conn
|> put_req_header("accept-language", "fr")
|> AbsintheHelpers.graphql_query(
query: @create_user_mutation,
variables: @user_creation
@@ -423,6 +426,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
res =
conn
|> put_req_header("accept-language", "fr")
|> AbsintheHelpers.graphql_query(
query: @register_person_mutation,
variables: @user_creation
@@ -447,6 +451,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do
insert(:actor, preferred_username: "myactor")
conn
|> put_req_header("accept-language", "fr")
|> AbsintheHelpers.graphql_query(
query: @create_user_mutation,
variables: @user_creation

View File

@@ -1,39 +0,0 @@
defmodule Mobilizon.Web.GettextTest do
use ExUnit.Case, async: true
alias Mobilizon.Config
alias Mobilizon.Web.Gettext, as: GettextBackend
describe "test determine_best_locale/1" do
setup do
Config.put([:instance, :default_language], "en")
:ok
end
test "with empty string returns the default locale" do
assert GettextBackend.determine_best_locale("") == "en"
end
test "with empty string returns the default configured locale" do
Config.put([:instance, :default_language], "es")
assert GettextBackend.determine_best_locale("") == "es"
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)
assert GettextBackend.determine_best_locale("") == "en"
end
test "returns fallback with an unexisting locale" do
assert GettextBackend.determine_best_locale("yolo") == "en"
end
test "maps the correct part if the locale has multiple ones" do
assert GettextBackend.determine_best_locale("fr_CA") == "fr"
end
test "returns the locale if valid" do
assert GettextBackend.determine_best_locale("es") == "es"
end
end
end

View File

@@ -0,0 +1,35 @@
# Portions of this file are derived from Pleroma:
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mobilizon.Web.Plugs.DetectLocalePlugTest do
use ExUnit.Case, async: true
use Plug.Test
alias Mobilizon.Web.Plugs.DetectLocalePlug
alias Plug.Conn
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"
)
|> DetectLocalePlug.call([])
assert %{detected_locale: "ru"} == conn.assigns
end
test "returns empty string if `accept-language` header is empty" do
conn =
:get
|> conn("/cofe")
|> Conn.put_req_header("accept-language", "tlh")
|> DetectLocalePlug.call([])
assert %{detected_locale: nil} == conn.assigns
end
end

View File

@@ -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