mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-02 04:22:40 +00:00
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.
33 lines
926 B
Ruby
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
|