discourse-translator/app/jobs/regular/translate_translatable.rb
Natalie Tay 7d411e458b
FEATURE: Translates every post to automatic_translation_target_languages (#207)
Introduces two site settings which will automatically translate posts to the language:

automatic_translation_target_languages
automatic_translation_backfill_maximum_posts_per_hour (hidden)
2025-02-14 11:46:53 +08:00

25 lines
860 B
Ruby

# frozen_string_literal: true
module Jobs
class TranslateTranslatable < ::Jobs::Base
def execute(args)
return unless SiteSetting.translator_enabled
return if SiteSetting.automatic_translation_target_languages.blank?
translatable = args[:type].constantize.find_by(id: args[:translatable_id])
return if translatable.blank?
target_locales = SiteSetting.automatic_translation_target_languages.split("|")
target_locales.each do |target_locale|
"DiscourseTranslator::#{SiteSetting.translator}".constantize.translate(
translatable,
target_locale.to_sym,
)
end
topic_id, post_id =
translatable.is_a?(Post) ? [translatable.topic_id, translatable.id] : [translatable.id, 1]
MessageBus.publish("/topic/#{topic_id}", type: :revised, id: post_id)
end
end
end