Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/discourse_translator/automatic_translations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,39 @@ module DiscourseTranslator
class AutomaticTranslations
def inject(plugin)
plugin.on(:post_process_cooked) do |_, post|
if SiteSetting.automatic_translation_target_languages.present? && post.user_id > 0
if translatable?(post)
Jobs.enqueue(:translate_translatable, type: "Post", translatable_id: post.id)
end
end

plugin.on(:topic_created) do |topic|
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
if translatable?(topic)
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
end
end

plugin.on(:topic_edited) do |topic|
if SiteSetting.automatic_translation_target_languages.present? && topic.user_id > 0
if translatable?(topic)
Jobs.enqueue(:translate_translatable, type: "Topic", translatable_id: topic.id)
end
end
end

def translatable?(content)
return false if SiteSetting.automatic_translation_target_languages.blank?
return false if content.user_id <= 0
return false if SiteSetting.automatic_translation_backfill_rate <= 0
return true unless SiteSetting.automatic_translation_backfill_limit_to_public_content

public_categories = Category.where(read_restricted: false).pluck(:id)

if content.is_a(Post)
public_categories.include?(content.topic.category_id)
elsif content.is_a(Topic)
public_categories.include?(content.category_id)
else
false
end
end
end
end
Loading