discourse-ai/lib/translation/category_localizer.rb
Natalie Tay 6827147362
DEV: Add topic and post id when using completions for traceability to AiApiAuditLog (#1414)
The AiApiAuditLog per translation event doesn't trace back easily to a post or topic.

This commit adds support to that, and also switches the translators to named arguments rather than positional arguments.
2025-06-06 23:24:24 +08:00

33 lines
926 B
Ruby

# frozen_string_literal: true
module DiscourseAi
module Translation
class CategoryLocalizer
def self.localize(category, target_locale = I18n.locale)
return if category.blank? || target_locale.blank?
target_locale = target_locale.to_s.sub("-", "_")
translated_name = ShortTextTranslator.new(text: category.name, target_locale:).translate
translated_description =
if category.description.present?
PostRawTranslator.new(text: category.description, target_locale:).translate
else
""
end
localization =
CategoryLocalization.find_or_initialize_by(
category_id: category.id,
locale: target_locale,
)
localization.name = translated_name
localization.description = translated_description
localization.save!
localization
end
end
end
end