File tree Expand file tree Collapse file tree 4 files changed +42
-5
lines changed Expand file tree Collapse file tree 4 files changed +42
-5
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11# frozen_string_literal: true
22module 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
Original file line number Diff line number Diff line change 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
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
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
Original file line number Diff line number Diff line change 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 (
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
You can’t perform that action at this time.
0 commit comments