Skip to content

Commit cf11bde

Browse files
authored
UX: Remove flash of 🌐 when the post has yet to have its language determined (#229)
When a post is just created, we see the 🌐 appear for a brief moment, then disappear. We typically want to show the 🌐 in case the post is not in the same language as the reader's. This works really well for old posts that do not have translation information. But since now all posts will be sent for locale detection on-create, it is a matter of seconds that the post's locale is determined. This commit adds a buffer of 10 seconds that will prevent the 🌐 from appearing if 1. it was just created and 2. it doesn't have the locale yet.
1 parent 2b770e4 commit cf11bde

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)
@@ -24,6 +26,10 @@ def can_translate?(post)
2426
return false if !user_group_allow_translate?
2527
return false if post.locale_matches?(I18n.locale)
2628

29+
# we want to return false if the post is created within a short buffer ago,
30+
# this prevents the 🌐from appearing and then disappearing if the lang is same as user's lang
31+
return false if post.created_at > POST_DETECTION_BUFFER.ago && post.detected_locale.blank?
32+
2733
if SiteSetting.experimental_topic_translation
2834
post.translation_for(I18n.locale).nil?
2935
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
@@ -160,6 +160,22 @@
160160
expect(guardian.can_translate?(post)).to eq(false)
161161
end
162162

163+
it "allows translation depending on when the post is created" do
164+
SiteSetting.restrict_translation_by_group = "#{group.id}"
165+
166+
post.update(created_at: Time.now)
167+
expect(guardian.can_translate?(post)).to eq(false)
168+
169+
post.set_detected_locale("jp")
170+
expect(guardian.can_translate?(post)).to eq(true)
171+
172+
post.update(created_at: 5.minutes.ago)
173+
expect(guardian.can_translate?(post)).to eq(true)
174+
175+
post.set_detected_locale("pt")
176+
expect(guardian.can_translate?(post)).to eq(false)
177+
end
178+
163179
describe "when experimental_topic_translation enabled" do
164180
before { SiteSetting.experimental_topic_translation = true }
165181

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)