2025-05-29 17:28:06 +08:00
|
|
|
# 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?
|
|
|
|
|
2025-06-06 23:24:24 +08:00
|
|
|
target_locale = target_locale.to_s.sub("-", "_")
|
2025-05-29 17:28:06 +08:00
|
|
|
|
2025-06-06 23:24:24 +08:00
|
|
|
translated_name = ShortTextTranslator.new(text: category.name, target_locale:).translate
|
2025-05-29 17:28:06 +08:00
|
|
|
translated_description =
|
2025-06-06 22:41:48 +08:00
|
|
|
if category.description.present?
|
2025-06-06 23:24:24 +08:00
|
|
|
PostRawTranslator.new(text: category.description, target_locale:).translate
|
2025-06-06 22:41:48 +08:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2025-05-29 17:28:06 +08:00
|
|
|
|
|
|
|
localization =
|
|
|
|
CategoryLocalization.find_or_initialize_by(
|
|
|
|
category_id: category.id,
|
2025-06-06 23:24:24 +08:00
|
|
|
locale: target_locale,
|
2025-05-29 17:28:06 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
localization.name = translated_name
|
|
|
|
localization.description = translated_description
|
|
|
|
localization.save!
|
|
|
|
localization
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|