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 )
@@ -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
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
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
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