Support multiple locales formally
elixir gettext current does not fully support fallback to another language [0]. But it might in the future. We adapt it so that all languages in Accept-Language headers are received by Pleroma.Web.Gettext. User.languages is now a comma-separated list. [0]: https://github.com/elixir-gettext/gettext/issues/303
This commit is contained in:
parent
d3f3f30c6a
commit
7ea330b4fe
3 changed files with 89 additions and 17 deletions
|
|
@ -16,7 +16,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
assert %{locale: "en"} = conn.assigns
|
||||
end
|
||||
|
||||
test "use supported locale from `accept-language`" do
|
||||
|
|
@ -30,7 +30,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru"} == conn.assigns
|
||||
assert %{locale: "ru"} = conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to the general language if a variant is not supported" do
|
||||
|
|
@ -44,7 +44,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru"} == conn.assigns
|
||||
assert %{locale: "ru"} = conn.assigns
|
||||
end
|
||||
|
||||
test "use supported locale with specifiers from `accept-language`" do
|
||||
|
|
@ -58,7 +58,21 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "zh_Hans" == Gettext.get_locale()
|
||||
assert %{locale: "zh_Hans"} == conn.assigns
|
||||
assert %{locale: "zh_Hans"} = conn.assigns
|
||||
end
|
||||
|
||||
test "it assigns all supported locales" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru", locales: ["ru", "fr", "en"]} = conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to some variant of the language if the unqualified language is not supported" do
|
||||
|
|
@ -87,7 +101,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "zh_Hans" == Gettext.get_locale()
|
||||
assert %{locale: "zh_Hans"} == conn.assigns
|
||||
assert %{locale: "zh_Hans"} = conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
|
||||
|
|
@ -102,7 +116,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru"} == conn.assigns
|
||||
assert %{locale: "ru"} = conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to default if nothing is supported" do
|
||||
|
|
@ -117,7 +131,7 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
assert %{locale: "en"} = conn.assigns
|
||||
end
|
||||
|
||||
test "use default locale if locale from `accept-language` is not supported" do
|
||||
|
|
@ -128,6 +142,6 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
assert %{locale: "en"} = conn.assigns
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue