2019-07-09 11:30:15 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-26 06:11:42 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 11:30:15 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-23 15:16:47 +00:00
|
|
|
defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
|
2019-07-09 11:30:15 +00:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
use Plug.Test
|
|
|
|
|
2020-06-24 06:24:29 +00:00
|
|
|
alias Pleroma.Web.Plugs.SetLocalePlug
|
2019-07-10 09:28:24 +00:00
|
|
|
alias Plug.Conn
|
2019-07-09 11:30:15 +00:00
|
|
|
|
|
|
|
test "default locale is `en`" do
|
|
|
|
conn =
|
|
|
|
:get
|
|
|
|
|> conn("/cofe")
|
|
|
|
|> SetLocalePlug.call([])
|
|
|
|
|
|
|
|
assert "en" == Gettext.get_locale()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "en"} = conn.assigns
|
2019-07-09 11:30:15 +00:00
|
|
|
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([])
|
|
|
|
|
|
|
|
assert "ru" == Gettext.get_locale()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "ru"} = conn.assigns
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
|
2022-03-03 01:04:30 +00:00
|
|
|
test "fallback to the general language if a variant is not supported" do
|
|
|
|
conn =
|
|
|
|
:get
|
|
|
|
|> conn("/cofe")
|
|
|
|
|> Conn.put_req_header(
|
|
|
|
"accept-language",
|
|
|
|
"ru-CA;q=0.9, en;q=0.8, *;q=0.5"
|
|
|
|
)
|
|
|
|
|> SetLocalePlug.call([])
|
|
|
|
|
|
|
|
assert "ru" == Gettext.get_locale()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "ru"} = conn.assigns
|
2022-03-03 01:04:30 +00:00
|
|
|
end
|
|
|
|
|
2022-02-21 22:54:18 +00:00
|
|
|
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()
|
2022-03-03 07:03:44 +00:00
|
|
|
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
|
2022-02-21 22:54:18 +00:00
|
|
|
end
|
|
|
|
|
2022-03-03 07:31:36 +00:00
|
|
|
test "it assigns all supported locales in cookie" do
|
|
|
|
conn =
|
|
|
|
:get
|
|
|
|
|> conn("/cofe")
|
|
|
|
|> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans,uk,zh-Hant")
|
|
|
|
|> 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 "zh_Hans" == Gettext.get_locale()
|
2022-03-03 14:40:18 +00:00
|
|
|
|
|
|
|
assert %{locale: "zh_Hans", locales: ["zh_Hans", "uk", "zh_Hant", "ru", "fr", "en"]} =
|
|
|
|
conn.assigns
|
2022-03-03 07:31:36 +00:00
|
|
|
end
|
|
|
|
|
Fallback to a variant if the language in general is not supported
For an example, here, zh is not supported, but zh_Hans and zh_Hant
are. If the user asks for zh, we should choose a variant for them
instead of fallbacking to default.
Some browsers (e.g. Firefox) does not allow users to customize
their language codes. For example, there is no zh-Hans, but only
zh, zh-CN, zh-TW, zh-HK, etc. This provides a workaround for
those users suffering from bad design decisions.
2022-03-03 00:59:11 +00:00
|
|
|
test "fallback to some variant of the language if the unqualified language is not supported" do
|
|
|
|
conn =
|
|
|
|
:get
|
|
|
|
|> conn("/cofe")
|
|
|
|
|> Conn.put_req_header(
|
|
|
|
"accept-language",
|
|
|
|
"zh;q=0.9, en;q=0.8, *;q=0.5"
|
|
|
|
)
|
|
|
|
|> SetLocalePlug.call([])
|
|
|
|
|
|
|
|
assert "zh_" <> _ = Gettext.get_locale()
|
|
|
|
assert %{locale: "zh_" <> _} = conn.assigns
|
|
|
|
end
|
|
|
|
|
2022-02-21 22:54:18 +00:00
|
|
|
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()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "zh_Hans"} = conn.assigns
|
2022-02-21 22:54:18 +00:00
|
|
|
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()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "ru"} = conn.assigns
|
2022-02-21 22:54:18 +00:00
|
|
|
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()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "en"} = conn.assigns
|
2019-07-09 11:30:15 +00:00
|
|
|
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()
|
2022-03-03 07:03:44 +00:00
|
|
|
assert %{locale: "en"} = conn.assigns
|
2019-07-09 11:30:15 +00:00
|
|
|
end
|
|
|
|
end
|