discourse-ai/lib/translation/post_localizer.rb
Natalie Tay b5e8277083
DEV: Move AI translation feature into an AI Feature (#1424)
This PR moves translations into an AI Feature

See https://github.com/discourse/discourse-ai/pull/1424 for screenshots
2025-06-13 10:17:27 +08:00

29 lines
905 B
Ruby

# frozen_string_literal: true
module DiscourseAi
module Translation
class PostLocalizer
def self.localize(post, target_locale = I18n.locale)
return if post.blank? || target_locale.blank? || post.locale == target_locale.to_s
target_locale = target_locale.to_s.sub("-", "_")
translated_raw =
ContentSplitter
.split(post.raw)
.map { |text| PostRawTranslator.new(text:, target_locale:, post:).translate }
.join("")
localization =
PostLocalization.find_or_initialize_by(post_id: post.id, locale: target_locale)
localization.raw = translated_raw
localization.cooked = PrettyText.cook(translated_raw)
localization.post_version = post.version
localization.localizer_user_id = Discourse.system_user.id
localization.save!
localization
end
end
end
end