1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.SetLocalePlugTest do
6 use ExUnit.Case, async: true
9 alias Pleroma.Web.Plugs.SetLocalePlug
12 test "default locale is `en`" do
16 |> SetLocalePlug.call([])
18 assert "en" == Gettext.get_locale()
19 assert %{locale: "en"} = conn.assigns
22 test "use supported locale from `accept-language`" do
26 |> Conn.put_req_header(
28 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
30 |> SetLocalePlug.call([])
32 assert "ru" == Gettext.get_locale()
33 assert %{locale: "ru"} = conn.assigns
36 test "fallback to the general language if a variant is not supported" do
40 |> Conn.put_req_header(
42 "ru-CA;q=0.9, en;q=0.8, *;q=0.5"
44 |> SetLocalePlug.call([])
46 assert "ru" == Gettext.get_locale()
47 assert %{locale: "ru"} = conn.assigns
50 test "use supported locale with specifiers from `accept-language`" do
54 |> Conn.put_req_header(
56 "zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
58 |> SetLocalePlug.call([])
60 assert "zh_Hans" == Gettext.get_locale()
61 assert %{locale: "zh_Hans"} = conn.assigns
64 test "it assigns all supported locales" do
68 |> Conn.put_req_header(
70 "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
72 |> SetLocalePlug.call([])
74 assert "ru" == Gettext.get_locale()
75 assert %{locale: "ru", locales: ["ru", "fr", "en"]} = conn.assigns
78 test "it assigns all supported locales in cookie" do
82 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans,uk,zh-Hant")
83 |> Conn.put_req_header(
85 "ru, fr-CH, fr;q=0.9, en;q=0.8, x-unsupported;q=0.8, *;q=0.5"
87 |> SetLocalePlug.call([])
89 assert "zh_Hans" == Gettext.get_locale()
91 assert %{locale: "zh_Hans", locales: ["zh_Hans", "uk", "zh_Hant", "ru", "fr", "en"]} =
95 test "fallback to some variant of the language if the unqualified language is not supported" do
99 |> Conn.put_req_header(
101 "zh;q=0.9, en;q=0.8, *;q=0.5"
103 |> SetLocalePlug.call([])
105 assert "zh_" <> _ = Gettext.get_locale()
106 assert %{locale: "zh_" <> _} = conn.assigns
109 test "use supported locale from cookie" do
113 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
114 |> Conn.put_req_header(
116 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
118 |> SetLocalePlug.call([])
120 assert "zh_Hans" == Gettext.get_locale()
121 assert %{locale: "zh_Hans"} = conn.assigns
124 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
128 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
129 |> Conn.put_req_header(
131 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
133 |> SetLocalePlug.call([])
135 assert "ru" == Gettext.get_locale()
136 assert %{locale: "ru"} = conn.assigns
139 test "fallback to default if nothing is supported" do
143 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
144 |> Conn.put_req_header(
148 |> SetLocalePlug.call([])
150 assert "en" == Gettext.get_locale()
151 assert %{locale: "en"} = conn.assigns
154 test "use default locale if locale from `accept-language` is not supported" do
158 |> Conn.put_req_header("accept-language", "tlh")
159 |> SetLocalePlug.call([])
161 assert "en" == Gettext.get_locale()
162 assert %{locale: "en"} = conn.assigns