Skip to content

Commit cbfc71d

Browse files
DEV: Disable automatic translation via reactive events when automatic rate is zero (#258)
Also makes reactive translation respect limiting translation to public content setting Co-authored-by: Keegan George <[email protected]>
1 parent 5dbe241 commit cbfc71d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/discourse_translator/automatic_translations.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,39 @@ module DiscourseTranslator
44
class AutomaticTranslations
55
def inject(plugin)
66
plugin.on(:post_process_cooked) do |_, post|
7-
if SiteSetting.automatic_translation_target_languages.present? && post.user_id > 0
7+
if translatable?(post)
88
Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id)
99
end
1010
end
1111

1212
plugin.on(:topic_created) do |topic|
13-
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
13+
if translatable?(topic)
1414
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
1515
end
1616
end
1717

1818
plugin.on(:topic_edited) do |topic|
19-
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
19+
if translatable?(topic)
2020
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
2121
end
2222
end
2323
end
24+
25+
def translatable?(content)
26+
return false if SiteSetting.automatic_translation_target_languages.blank?
27+
return false if content.user_id <= 0
28+
return false if SiteSetting.automatic_translation_backfill_rate <= 0
29+
return true unless SiteSetting.automatic_translation_backfill_limit_to_public_content
30+
31+
public_categories = Category.where(read_restricted: false).pluck(:id)
32+
33+
if content.is_a(Post)
34+
public_categories.include?(content.topic.category_id)
35+
elsif content.is_a(Topic)
36+
public_categories.include?(content.category_id)
37+
else
38+
false
39+
end
40+
end
2441
end
2542
end

0 commit comments

Comments
 (0)