2025-05-29 17:28:06 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class LocalizeCategories < ::Jobs::Base
|
|
|
|
cluster_concurrency 1
|
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
BATCH_SIZE = 50
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
return if !SiteSetting.discourse_ai_enabled
|
|
|
|
return if !SiteSetting.ai_translation_enabled
|
|
|
|
|
2025-06-19 12:23:56 +08:00
|
|
|
locales = SiteSetting.content_localization_supported_locales.split("|")
|
2025-05-29 17:28:06 +08:00
|
|
|
return if locales.blank?
|
|
|
|
|
|
|
|
cat_id = args[:from_category_id] || Category.order(:id).first&.id
|
|
|
|
last_id = nil
|
|
|
|
|
2025-06-06 22:41:48 +08:00
|
|
|
categories =
|
|
|
|
Category.where("id >= ? AND locale IS NOT NULL", cat_id).order(:id).limit(BATCH_SIZE)
|
2025-05-29 17:28:06 +08:00
|
|
|
return if categories.empty?
|
|
|
|
|
|
|
|
categories.each do |category|
|
|
|
|
if SiteSetting.ai_translation_backfill_limit_to_public_content && category.read_restricted?
|
|
|
|
last_id = category.id
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2025-06-06 22:41:48 +08:00
|
|
|
locales.each do |locale|
|
|
|
|
localization = category.category_localizations.find_by(locale:)
|
|
|
|
|
|
|
|
if locale == category.locale && localization
|
|
|
|
localization.destroy
|
|
|
|
else
|
|
|
|
next if locale == category.locale
|
2025-05-29 17:28:06 +08:00
|
|
|
begin
|
|
|
|
DiscourseAi::Translation::CategoryLocalizer.localize(category, locale)
|
|
|
|
rescue FinalDestination::SSRFDetector::LookupFailedError
|
|
|
|
# do nothing, there are too many sporadic lookup failures
|
|
|
|
rescue => e
|
|
|
|
DiscourseAi::Translation::VerboseLogger.log(
|
|
|
|
"Failed to translate category #{category.id} to #{locale}: #{e.message}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
last_id = category.id
|
|
|
|
end
|
|
|
|
|
|
|
|
if categories.size == BATCH_SIZE
|
|
|
|
Jobs.enqueue_in(10.seconds, :localize_categories, from_category_id: last_id + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|