Skip to content

Commit a515928

Browse files
committed
UX: Remove flash of 🌐 when the post has yet to have its language determined
1 parent 01744ed commit a515928

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

ā€Žlib/discourse_translator/dual_text_translation.rbā€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def inject(plugin)
1515
end
1616

1717
plugin.on(:topic_created) do |topic|
18-
if SiteSetting.automatic_translation_target_languages.blank? && topic.user_id > 0
18+
if SiteSetting.automatic_translation_target_languages.blank? &&
19+
Guardian.new.can_detect_language?(topic.first_post) && topic.user_id > 0
1920
Jobs.enqueue(:detect_translatable_language, type: "Topic", translatable_id: topic.id)
2021
end
2122
end

ā€Žlib/discourse_translator/guardian_extension.rbā€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22
module DiscourseTranslator::GuardianExtension
3+
POST_DETECTION_BUFFER = 10.seconds
4+
35
def user_group_allow_translate?
46
return false if !current_user
57
current_user.in_any_groups?(SiteSetting.restrict_translation_by_group_map)
@@ -23,6 +25,10 @@ def can_translate?(post)
2325
return false if !user_group_allow_translate?
2426
return false if post.locale_matches?(I18n.locale)
2527

28+
# we want to return false if the post is created within a short buffer ago,
29+
# this prevents the 🌐from appearing and then disappearing if the lang is same as user's lang
30+
return false if post.created_at > POST_DETECTION_BUFFER.ago && post.detected_locale.blank?
31+
2632
if SiteSetting.experimental_topic_translation
2733
post.translation_for(I18n.locale).nil?
2834
else

ā€Žspec/lib/guardian_extension_spec.rbā€Ž

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
describe "#can_translate?" do
118118
fab!(:group)
119119
fab!(:user) { Fabricate(:user, locale: "en", groups: [group]) }
120-
fab!(:post)
120+
fab!(:post) { Fabricate(:post, created_at: 5.minutes.ago) }
121121

122122
let(:guardian) { Guardian.new(user) }
123123

@@ -127,7 +127,7 @@
127127
expect(guardian.can_translate?(post)).to eq(false)
128128
end
129129

130-
describe "when translator enabled" do
130+
describe "when translator enabled and user locale is pt" do
131131
before do
132132
SiteSetting.translator_enabled = true
133133
I18n.locale = :pt
@@ -155,6 +155,22 @@
155155
expect(guardian.can_translate?(post)).to eq(false)
156156
end
157157

158+
it "allows translation depending on when the post is created" do
159+
SiteSetting.restrict_translation_by_group = "#{group.id}"
160+
161+
post.update(created_at: Time.now)
162+
expect(guardian.can_translate?(post)).to eq(false)
163+
164+
post.set_detected_locale("jp")
165+
expect(guardian.can_translate?(post)).to eq(true)
166+
167+
post.update(created_at: 5.minutes.ago)
168+
expect(guardian.can_translate?(post)).to eq(true)
169+
170+
post.set_detected_locale("pt")
171+
expect(guardian.can_translate?(post)).to eq(false)
172+
end
173+
158174
describe "when experimental_topic_translation enabled" do
159175
before { SiteSetting.experimental_topic_translation = true }
160176

ā€Žspec/models/post_spec.rbā€Ž

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,20 @@
7070

7171
describe "queueing post for language detection" do
7272
fab!(:group)
73-
fab!(:topic)
7473
fab!(:user) { Fabricate(:user, groups: [group]) }
7574

7675
it "queues the post for language detection when user and posts are in the right group" do
7776
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
7877

79-
post = Fabricate(:post, user: user)
78+
post =
79+
PostCreator.new(
80+
user,
81+
{
82+
title: "a topic about cats",
83+
raw: "tomtom is a cat",
84+
category: Fabricate(:category).id,
85+
},
86+
).create
8087
CookedPostProcessor.new(post).post_process
8188

8289
expect_job_enqueued(
@@ -86,6 +93,13 @@
8693
translatable_id: post.id,
8794
},
8895
)
96+
expect_job_enqueued(
97+
job: :detect_translatable_language,
98+
args: {
99+
type: "Topic",
100+
translatable_id: post.topic_id,
101+
},
102+
)
89103
end
90104

91105
it "does not queue bot posts for language detection" do

0 commit comments

Comments
Ā (0)