1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 "fallback to some variant of the language if the unqualified language is not supported" do
82 |> Conn.put_req_header(
84 "zh;q=0.9, en;q=0.8, *;q=0.5"
86 |> SetLocalePlug.call([])
88 assert "zh_" <> _ = Gettext.get_locale()
89 assert %{locale: "zh_" <> _} = conn.assigns
92 test "use supported locale from cookie" do
96 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
97 |> Conn.put_req_header(
99 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
101 |> SetLocalePlug.call([])
103 assert "zh_Hans" == Gettext.get_locale()
104 assert %{locale: "zh_Hans"} = conn.assigns
107 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
111 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
112 |> Conn.put_req_header(
114 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
116 |> SetLocalePlug.call([])
118 assert "ru" == Gettext.get_locale()
119 assert %{locale: "ru"} = conn.assigns
122 test "fallback to default if nothing is supported" do
126 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
127 |> Conn.put_req_header(
131 |> SetLocalePlug.call([])
133 assert "en" == Gettext.get_locale()
134 assert %{locale: "en"} = conn.assigns
137 test "use default locale if locale from `accept-language` is not supported" do
141 |> Conn.put_req_header("accept-language", "tlh")
142 |> SetLocalePlug.call([])
144 assert "en" == Gettext.get_locale()
145 assert %{locale: "en"} = conn.assigns