Skip to content

Commit 2748ef5

Browse files
committed
FIX: Ensure old feature works with new and show translate button in correct scenario
1 parent 53cdbc8 commit 2748ef5

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

lib/discourse_translator/guardian_extension.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ def can_detect_language?(post)
2020
end
2121

2222
def can_translate?(post)
23-
return false if !user_group_allow_translate?
23+
return false unless 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.translation_for(I18n.locale).present?
27+
return false if post.locale_matches?(I18n.locale)
28+
true
29+
else
30+
poster_group_allow_translate?(post)
31+
end
3432
end
3533
end

spec/lib/guardian_extension_spec.rb

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -130,42 +130,92 @@
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 "can translate when post does not have translation" do
170+
post.set_detected_locale("jp")
171+
172+
expect(guardian.can_translate?(post)).to eq(true)
173+
end
174+
end
175+
end
138176
end
139177
end
140178

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}"
179+
describe "when experimental topic translation disabled" do
180+
before { SiteSetting.experimental_topic_translation = false }
181+
182+
describe "anon user" do
183+
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
144184

145-
expect(guardian.can_translate?(post)).to eq(false)
185+
it "cannot translate" do
186+
expect(Guardian.new.can_translate?(post)).to eq(false)
187+
end
146188
end
147189

148-
describe "user is in restrict_translation_by_group" do
149-
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
190+
describe "logged in user" do
191+
it "cannot translate when user is not in restrict_translation_by_group" do
192+
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
150193

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

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
197+
describe "user is in restrict_translation_by_group" do
198+
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
158199

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

162-
expect(guardian.can_translate?(post)).to eq(false)
163-
end
203+
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
204+
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"
205+
206+
expect(guardian.can_translate?(post)).to eq(false)
207+
end
208+
209+
it "can translate if poster is in restrict_translation_by_poster_group" do
210+
poster = post.user
211+
poster_group = Fabricate(:group, users: [poster])
164212

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

168-
expect(guardian.can_translate?(post)).to eq(true)
216+
SiteSetting.restrict_translation_by_poster_group = ""
217+
expect(guardian.can_translate?(post)).to eq(true)
218+
end
169219
end
170220
end
171221
end

0 commit comments

Comments
 (0)