move to 2.5.5
[anni] / test / pleroma / web / gettext_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.GettextTest do
6   use ExUnit.Case
7
8   require Pleroma.Web.Gettext
9
10   test "put_locales/1: set the first in the list to Gettext's locale" do
11     Pleroma.Web.Gettext.put_locales(["zh_Hans", "en_test"])
12
13     assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
14   end
15
16   test "with_locales/2: reset locale on exit" do
17     old_first_locale = Gettext.get_locale(Pleroma.Web.Gettext)
18     old_locales = Pleroma.Web.Gettext.get_locales()
19
20     Pleroma.Web.Gettext.with_locales ["zh_Hans", "en_test"] do
21       assert "zh_Hans" == Gettext.get_locale(Pleroma.Web.Gettext)
22       assert ["zh_Hans", "en_test"] == Pleroma.Web.Gettext.get_locales()
23     end
24
25     assert old_first_locale == Gettext.get_locale(Pleroma.Web.Gettext)
26     assert old_locales == Pleroma.Web.Gettext.get_locales()
27   end
28
29   describe "handle_missing_translation/5" do
30     test "fallback to next locale if some translation is not available" do
31       Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
32         assert "xxYour account is awaiting approvalxx" ==
33                  Pleroma.Web.Gettext.dpgettext(
34                    "static_pages",
35                    "approval pending email subject",
36                    "Your account is awaiting approval"
37                  )
38       end
39     end
40
41     test "putting en locale at the front should not make gettext fallback unexpectedly" do
42       Pleroma.Web.Gettext.with_locales ["en", "en_test"] do
43         assert "Your account is awaiting approval" ==
44                  Pleroma.Web.Gettext.dpgettext(
45                    "static_pages",
46                    "approval pending email subject",
47                    "Your account is awaiting approval"
48                  )
49       end
50     end
51
52     test "duplicated locale in list should not result in infinite loops" do
53       Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
54         assert "xxYour account is awaiting approvalxx" ==
55                  Pleroma.Web.Gettext.dpgettext(
56                    "static_pages",
57                    "approval pending email subject",
58                    "Your account is awaiting approval"
59                  )
60       end
61     end
62
63     test "direct interpolation" do
64       Pleroma.Web.Gettext.with_locales ["en_test"] do
65         assert "xxYour digest from some instancexx" ==
66                  Pleroma.Web.Gettext.dpgettext(
67                    "static_pages",
68                    "digest email subject",
69                    "Your digest from %{instance_name}",
70                    instance_name: "some instance"
71                  )
72       end
73     end
74
75     test "fallback with interpolation" do
76       Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
77         assert "xxYour digest from some instancexx" ==
78                  Pleroma.Web.Gettext.dpgettext(
79                    "static_pages",
80                    "digest email subject",
81                    "Your digest from %{instance_name}",
82                    instance_name: "some instance"
83                  )
84       end
85     end
86
87     test "fallback to msgid" do
88       Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
89         assert "Your digest from some instance" ==
90                  Pleroma.Web.Gettext.dpgettext(
91                    "static_pages",
92                    "digest email subject",
93                    "Your digest from %{instance_name}",
94                    instance_name: "some instance"
95                  )
96       end
97     end
98   end
99
100   describe "handle_missing_plural_translation/7" do
101     test "direct interpolation" do
102       Pleroma.Web.Gettext.with_locales ["en_test"] do
103         assert "xx1 New Followerxx" ==
104                  Pleroma.Web.Gettext.dpngettext(
105                    "static_pages",
106                    "new followers count header",
107                    "%{count} New Follower",
108                    "%{count} New Followers",
109                    1,
110                    count: 1
111                  )
112
113         assert "xx5 New Followersxx" ==
114                  Pleroma.Web.Gettext.dpngettext(
115                    "static_pages",
116                    "new followers count header",
117                    "%{count} New Follower",
118                    "%{count} New Followers",
119                    5,
120                    count: 5
121                  )
122       end
123     end
124
125     test "fallback with interpolation" do
126       Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
127         assert "xx1 New Followerxx" ==
128                  Pleroma.Web.Gettext.dpngettext(
129                    "static_pages",
130                    "new followers count header",
131                    "%{count} New Follower",
132                    "%{count} New Followers",
133                    1,
134                    count: 1
135                  )
136
137         assert "xx5 New Followersxx" ==
138                  Pleroma.Web.Gettext.dpngettext(
139                    "static_pages",
140                    "new followers count header",
141                    "%{count} New Follower",
142                    "%{count} New Followers",
143                    5,
144                    count: 5
145                  )
146       end
147     end
148
149     test "fallback to msgid" do
150       Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
151         assert "1 New Follower" ==
152                  Pleroma.Web.Gettext.dpngettext(
153                    "static_pages",
154                    "new followers count header",
155                    "%{count} New Follower",
156                    "%{count} New Followers",
157                    1,
158                    count: 1
159                  )
160
161         assert "5 New Followers" ==
162                  Pleroma.Web.Gettext.dpngettext(
163                    "static_pages",
164                    "new followers count header",
165                    "%{count} New Follower",
166                    "%{count} New Followers",
167                    5,
168                    count: 5
169                  )
170       end
171     end
172   end
173 end