mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-06-19 07:03:42 +08:00
This PR changes Discourse AI translations from an opt-in category model to an opt-out model: instead of translating only selected `ai_translation_target_categories`, it introduces `ai_translation_excluded_categories`, updates the admin UI copy and save flow, changes topic/post/category candidate selection and detection jobs to translate all non-excluded categories by default, and adds a migration that converts existing target-category settings into the equivalent excluded-category list for existing sites. It also updates all related specs. --------- Co-authored-by: discourse-patch-triage[bot] <272280883+discourse-patch-triage[bot]@users.noreply.github.com>
91 lines
2.8 KiB
Ruby
Vendored
91 lines
2.8 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Jobs
|
|
class DetectTranslatePost < ::Jobs::Base
|
|
cluster_concurrency 1
|
|
sidekiq_options retry: false
|
|
|
|
def execute(args)
|
|
return if !DiscourseAi::Translation.enabled?
|
|
return if args[:post_id].blank?
|
|
|
|
unless DiscourseAi::Translation.credits_available_for_post_detection?
|
|
Rails.logger.info(
|
|
"Translation skipped for post: insufficient credits. Will resume when credits reset.",
|
|
)
|
|
return
|
|
end
|
|
|
|
post = Post.find_by(id: args[:post_id])
|
|
return if post.blank? || post.raw.blank? || post.deleted_at.present?
|
|
|
|
force = args[:force] || false
|
|
return if post.user_id <= 0 && !force && !SiteSetting.ai_translation_include_bot_content
|
|
|
|
topic = post.topic
|
|
return if topic.blank?
|
|
|
|
if force
|
|
# no restrictions
|
|
elsif topic.archetype == Archetype.private_message
|
|
case SiteSetting.ai_translation_personal_messages
|
|
when "all"
|
|
# allow
|
|
when "group"
|
|
return unless TopicAllowedGroup.exists?(topic_id: topic.id)
|
|
else
|
|
return
|
|
end
|
|
else
|
|
return if DiscourseAi::Translation.category_excluded?(topic.category_id)
|
|
end
|
|
|
|
# the user may fill locale in manually
|
|
if (detected_locale = post.locale).blank?
|
|
begin
|
|
detected_locale = DiscourseAi::Translation::PostLocaleDetector.detect_locale(post)
|
|
rescue FinalDestination::SSRFDetector::LookupFailedError
|
|
# this job is non-critical
|
|
# the backfill job will handle failures
|
|
return
|
|
end
|
|
end
|
|
|
|
return if detected_locale.blank?
|
|
locales = DiscourseAi::Translation.locales
|
|
return if locales.blank?
|
|
|
|
existing_base_locales =
|
|
PostLocalization
|
|
.where(post_id: post.id)
|
|
.pluck(:locale)
|
|
.map { |l| l.split("_").first }
|
|
.to_set
|
|
|
|
locales.each do |locale|
|
|
next if LocaleNormalizer.is_same?(locale, detected_locale)
|
|
base_locale = locale.split("_").first
|
|
exists = existing_base_locales.include?(base_locale)
|
|
|
|
has_quota = DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)
|
|
next if !force && exists && !has_quota
|
|
|
|
localize(post, locale)
|
|
end
|
|
|
|
MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)
|
|
end
|
|
|
|
private
|
|
|
|
def localize(post, locale)
|
|
DiscourseAi::Translation::PostLocalizer.localize(post, locale)
|
|
rescue FinalDestination::SSRFDetector::LookupFailedError
|
|
# do nothing, there are too many sporadic lookup failures
|
|
rescue => e
|
|
DiscourseAi::Translation::VerboseLogger.log(
|
|
"Failed to translate post #{post.id} to #{locale}: #{e.message}\n\n#{e.backtrace[0..3].join("\n")}",
|
|
)
|
|
end
|
|
end
|
|
end
|