discourse-translator/app/jobs/regular/translate_translatable.rb
Natalie Tay a90d737160
FIX: Do not translate new posts if show=original (#271)
When a new post is created, and a user is in "show original" translation mode when inline translation is enabled, the new post comes in translated.

This commit fixes that by registering a custom post message callback, and checking if the user is in "show original".
2025-04-10 10:02:52 +08:00

25 lines
877 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_provider}".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: :translated_post, id: post_id)
end
end
end