Support fallbacking to other languages
[akkoma] / 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 "duplicated locale in list should not result in infinite loops" do
42 Pleroma.Web.Gettext.with_locales ["x_unsupported", "x_unsupported", "en_test"] do
43 assert "xxYour account is awaiting approvalxx" ==
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 "direct interpolation" do
53 Pleroma.Web.Gettext.with_locales ["en_test"] do
54 assert "xxYour digest from some instancexx" ==
55 Pleroma.Web.Gettext.dpgettext(
56 "static_pages",
57 "digest email subject",
58 "Your digest from %{instance_name}",
59 instance_name: "some instance"
60 )
61 end
62 end
63
64 test "fallback with interpolation" do
65 Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
66 assert "xxYour digest from some instancexx" ==
67 Pleroma.Web.Gettext.dpgettext(
68 "static_pages",
69 "digest email subject",
70 "Your digest from %{instance_name}",
71 instance_name: "some instance"
72 )
73 end
74 end
75
76 test "fallback to msgid" do
77 Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
78 assert "Your digest from some instance" ==
79 Pleroma.Web.Gettext.dpgettext(
80 "static_pages",
81 "digest email subject",
82 "Your digest from %{instance_name}",
83 instance_name: "some instance"
84 )
85 end
86 end
87 end
88
89 describe "handle_missing_plural_translation/7" do
90 test "direct interpolation" do
91 Pleroma.Web.Gettext.with_locales ["en_test"] do
92 assert "xx1 New Followerxx" ==
93 Pleroma.Web.Gettext.dpngettext(
94 "static_pages",
95 "new followers count header",
96 "%{count} New Follower",
97 "%{count} New Followers",
98 1,
99 count: 1
100 )
101
102 assert "xx5 New Followersxx" ==
103 Pleroma.Web.Gettext.dpngettext(
104 "static_pages",
105 "new followers count header",
106 "%{count} New Follower",
107 "%{count} New Followers",
108 5,
109 count: 5
110 )
111 end
112 end
113
114 test "fallback with interpolation" do
115 Pleroma.Web.Gettext.with_locales ["x_unsupported", "en_test"] do
116 assert "xx1 New Followerxx" ==
117 Pleroma.Web.Gettext.dpngettext(
118 "static_pages",
119 "new followers count header",
120 "%{count} New Follower",
121 "%{count} New Followers",
122 1,
123 count: 1
124 )
125
126 assert "xx5 New Followersxx" ==
127 Pleroma.Web.Gettext.dpngettext(
128 "static_pages",
129 "new followers count header",
130 "%{count} New Follower",
131 "%{count} New Followers",
132 5,
133 count: 5
134 )
135 end
136 end
137
138 test "fallback to msgid" do
139 Pleroma.Web.Gettext.with_locales ["x_unsupported"] do
140 assert "1 New Follower" ==
141 Pleroma.Web.Gettext.dpngettext(
142 "static_pages",
143 "new followers count header",
144 "%{count} New Follower",
145 "%{count} New Followers",
146 1,
147 count: 1
148 )
149
150 assert "5 New Followers" ==
151 Pleroma.Web.Gettext.dpngettext(
152 "static_pages",
153 "new followers count header",
154 "%{count} New Follower",
155 "%{count} New Followers",
156 5,
157 count: 5
158 )
159 end
160 end
161 end
162 end