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 "use supported locale with specifiers from `accept-language`" do
40 |> Conn.put_req_header(
42 "zh-Hans;q=0.9, en;q=0.8, *;q=0.5"
44 |> SetLocalePlug.call([])
46 assert "zh_Hans" == Gettext.get_locale()
47 assert %{locale: "zh_Hans"} == conn.assigns
50 test "fallback to some variant of the language if the unqualified language is not supported" do
54 |> Conn.put_req_header(
56 "zh;q=0.9, en;q=0.8, *;q=0.5"
58 |> SetLocalePlug.call([])
60 assert "zh_" <> _ = Gettext.get_locale()
61 assert %{locale: "zh_" <> _} = conn.assigns
64 test "use supported locale from cookie" do
68 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans")
69 |> Conn.put_req_header(
71 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
73 |> SetLocalePlug.call([])
75 assert "zh_Hans" == Gettext.get_locale()
76 assert %{locale: "zh_Hans"} == conn.assigns
79 test "fallback to supported locale from `accept-language` if locale in cookie not supported" do
83 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
84 |> Conn.put_req_header(
86 "ru, fr-CH, fr;q=0.9, en;q=0.8, *;q=0.5"
88 |> SetLocalePlug.call([])
90 assert "ru" == Gettext.get_locale()
91 assert %{locale: "ru"} == conn.assigns
94 test "fallback to default if nothing is supported" do
98 |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "x-nonexist")
99 |> Conn.put_req_header(
103 |> SetLocalePlug.call([])
105 assert "en" == Gettext.get_locale()
106 assert %{locale: "en"} == conn.assigns
109 test "use default locale if locale from `accept-language` is not supported" do
113 |> Conn.put_req_header("accept-language", "tlh")
114 |> SetLocalePlug.call([])
116 assert "en" == Gettext.get_locale()
117 assert %{locale: "en"} == conn.assigns