Skip to content

Commit cdb3a59

Browse files
authored
FIX: Ensure old feature works with new and show translate button in correct scenarios (#215)
In the older implementation of translation, there exists these two site settings are part of the criteria that determines if 🌐 shows up per post * `restrict translation by group` - Only allowed groups can translate * `restrict translation by poster group` - Only allow translation of posts made by users in allowed groups. If empty, allow translations of posts from all users. In a recent update with experimental features, we updated the cases where 🌐 will show up and caused a regression where the 🌐 shows up for everyone in `restrict translation by group`, ignoring `restrict translation by poster group`. This commit makes sure these two site settings are adhered to when the experimental topic setting is turned off.
1 parent c66329d commit cdb3a59

File tree

3 files changed

+110
-66
lines changed

3 files changed

+110
-66
lines changed

lib/discourse_translator/guardian_extension.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ def can_detect_language?(post)
2222
def can_translate?(post)
2323
return false if !user_group_allow_translate?
2424

25-
locale = post.detected_locale
26-
return false if locale.nil?
27-
28-
# I18n.locale is a symbol e.g. :en_GB
29-
detected_lang = locale.to_s.sub("-", "_")
30-
detected_lang != I18n.locale.to_s &&
31-
"DiscourseTranslator::#{SiteSetting.translator}".constantize.language_supported?(
32-
detected_lang,
33-
)
25+
if SiteSetting.experimental_topic_translation
26+
return false if post.locale_matches?(I18n.locale)
27+
return false if post.translation_for(I18n.locale).present?
28+
true
29+
else
30+
return false if post.locale_matches?(I18n.locale)
31+
poster_group_allow_translate?(post)
32+
end
3433
end
3534
end

spec/lib/guardian_extension_spec.rb

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,111 @@
130130
describe "when translator enabled" do
131131
before { SiteSetting.translator_enabled = true }
132132

133-
describe "anon user" do
134-
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
133+
describe "when experimental_topic_translation enabled" do
134+
before { SiteSetting.experimental_topic_translation = true }
135135

136-
it "cannot translate" do
137-
expect(Guardian.new.can_translate?(post)).to eq(false)
136+
describe "anon user" do
137+
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
138+
139+
it "cannot translate" do
140+
expect(Guardian.new.can_translate?(post)).to eq(false)
141+
end
142+
end
143+
144+
describe "logged in user" do
145+
it "cannot translate when user is not in restrict_translation_by_group" do
146+
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
147+
148+
expect(guardian.can_translate?(post)).to eq(false)
149+
end
150+
151+
describe "user is in restrict_translation_by_group" do
152+
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
153+
154+
describe "locale is :pt" do
155+
before { I18n.stubs(:locale).returns(:pt) }
156+
157+
it "cannot translate when post detected locale matches i18n locale" do
158+
post.set_detected_locale("pt")
159+
160+
expect(guardian.can_translate?(post)).to eq(false)
161+
end
162+
163+
it "can translate when post's detected locale does not match i18n locale" do
164+
post.set_detected_locale("jp")
165+
166+
expect(guardian.can_translate?(post)).to eq(true)
167+
end
168+
169+
it "cannot translate when post has translation for user locale" do
170+
post.set_detected_locale("jp")
171+
post.set_translation("pt", "Olá, mundo!")
172+
173+
expect(guardian.can_translate?(post)).to eq(false)
174+
end
175+
176+
it "can translate when post does not have translation for user locale" do
177+
post.set_detected_locale("jp")
178+
179+
expect(guardian.can_translate?(post)).to eq(true)
180+
end
181+
end
182+
end
138183
end
139184
end
140185

141-
describe "logged in user" do
142-
it "cannot translate when user is not in restrict_translation_by_group" do
143-
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
186+
describe "when experimental topic translation disabled" do
187+
before { SiteSetting.experimental_topic_translation = false }
188+
189+
describe "anon user" do
190+
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
144191

145-
expect(guardian.can_translate?(post)).to eq(false)
192+
it "cannot translate" do
193+
expect(Guardian.new.can_translate?(post)).to eq(false)
194+
end
146195
end
147196

148-
describe "user is in restrict_translation_by_group" do
149-
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
197+
describe "logged in user" do
198+
it "cannot translate when user is not in restrict_translation_by_group" do
199+
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
150200

151-
describe "locale is :xx" do
152-
before { I18n.stubs(:locale).returns(:pt) }
201+
expect(guardian.can_translate?(post)).to eq(false)
202+
end
153203

154-
it "cannot translate when post does not have detected locale" do
155-
expect(post.detected_locale).to eq(nil)
156-
expect(guardian.can_translate?(post)).to eq(false)
157-
end
204+
describe "user is in restrict_translation_by_group" do
205+
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
158206

159-
it "cannot translate when post detected locale matches i18n locale" do
160-
post.set_detected_locale("pt")
207+
describe "locale is :pt" do
208+
before { I18n.stubs(:locale).returns(:pt) }
161209

162-
expect(guardian.can_translate?(post)).to eq(false)
163-
end
210+
it "cannot translate when post detected locale matches i18n locale" do
211+
post.set_detected_locale("pt")
212+
213+
expect(guardian.can_translate?(post)).to eq(false)
214+
end
215+
216+
it "can translate when post's detected locale does not match i18n locale" do
217+
post.set_detected_locale("jp")
218+
219+
expect(guardian.can_translate?(post)).to eq(true)
220+
end
221+
222+
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
223+
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"
224+
225+
expect(guardian.can_translate?(post)).to eq(false)
226+
end
227+
228+
it "can translate if poster is in restrict_translation_by_poster_group" do
229+
poster = post.user
230+
poster_group = Fabricate(:group, users: [poster])
164231

165-
it "can translate when post detected locale does not match i18n locale" do
166-
post.set_detected_locale("jp")
232+
SiteSetting.restrict_translation_by_poster_group = "#{poster_group.id}"
233+
expect(guardian.can_translate?(post)).to eq(true)
167234

168-
expect(guardian.can_translate?(post)).to eq(true)
235+
SiteSetting.restrict_translation_by_poster_group = ""
236+
expect(guardian.can_translate?(post)).to eq(true)
237+
end
169238
end
170239
end
171240
end

spec/serializers/post_serializer_spec.rb

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,15 @@
1919
end
2020

2121
describe "when translator enabled" do
22-
before { SiteSetting.translator_enabled = true }
23-
24-
describe "anon user" do
25-
let(:serializer) { PostSerializer.new(post, scope: Guardian.new) }
26-
27-
before do
28-
SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}"
29-
SiteSetting.restrict_translation_by_poster_group = ""
30-
end
22+
before do
23+
SiteSetting.translator_enabled = true
24+
SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}"
25+
SiteSetting.restrict_translation_by_poster_group = ""
26+
end
27+
let(:serializer) { PostSerializer.new(post, scope: Guardian.new) }
3128

32-
it "cannot translate" do
33-
expect(serializer.can_translate).to eq(false)
34-
end
29+
it "cannot translate for anon" do
30+
expect(serializer.can_translate).to eq(false)
3531
end
3632

3733
describe "logged in user" do
@@ -44,32 +40,12 @@
4440
end
4541

4642
describe "user is in restrict_translation_by_group" do
47-
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
48-
49-
it "cannot translate when post author is not in restrict_translation_by_poster_group" do
50-
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
51-
52-
expect(serializer.can_translate).to eq(false)
53-
end
54-
5543
describe "post author in restrict_translation_by_poster_group and locale is :xx" do
56-
before do
44+
it "can translate when post detected locale does not match i18n locale" do
45+
SiteSetting.restrict_translation_by_group = "#{group.id}"
5746
SiteSetting.restrict_translation_by_poster_group = "#{post_user_group.id}"
5847
I18n.stubs(:locale).returns(:pt)
59-
end
60-
61-
it "cannot translate when post does not have detected locale" do
62-
expect(post.detected_locale).to eq(nil)
63-
expect(serializer.can_translate).to eq(false)
64-
end
6548

66-
it "cannot translate when post detected locale matches i18n locale" do
67-
post.set_detected_locale("pt")
68-
69-
expect(serializer.can_translate).to eq(false)
70-
end
71-
72-
it "can translate when post detected locale does not match i18n locale" do
7349
post.set_detected_locale("jp")
7450

7551
expect(serializer.can_translate).to eq(true)

0 commit comments

Comments
 (0)