Prefer userLanguage cookie over Accept-Language header in detecting locale
https://git.pleroma.social/pleroma/pleroma-meta/-/issues/60
This commit is contained in:
parent
d91e9cee04
commit
0fd3695b9c
2 changed files with 87 additions and 1 deletions
|
|
@ -33,6 +33,65 @@ defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
|||
assert %{locale: "ru"} == conn.assigns
|
||||
end
|
||||
|
||||
test "use supported locale with specifiers from `accept-language`" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "zh_Hans" == Gettext.get_locale()
|
||||
assert %{locale: "zh_Hans"} == conn.assigns
|
||||
end
|
||||
|
||||
test "use supported locale from cookie" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "zh_Hans" == Gettext.get_locale()
|
||||
assert %{locale: "zh_Hans"} == conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "ru" == Gettext.get_locale()
|
||||
assert %{locale: "ru"} == conn.assigns
|
||||
end
|
||||
|
||||
test "fallback to default if nothing is supported" do
|
||||
conn =
|
||||
:get
|
||||
|> conn("/cofe")
|
||||
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
|
||||
|> Conn.put_req_header(
|
||||
"accept-language",
|
||||
"x-nonexist"
|
||||
)
|
||||
|> SetLocalePlug.call([])
|
||||
|
||||
assert "en" == Gettext.get_locale()
|
||||
assert %{locale: "en"} == conn.assigns
|
||||
end
|
||||
|
||||
test "use default locale if locale from `accept-language` is not supported" do
|
||||
conn =
|
||||
:get
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue