2025-05-29 17:28:06 +08:00
|
|
|
# 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
|
2025-06-06 23:24:24 +08:00
|
|
|
target_locale = target_locale.to_s.sub("-", "_")
|
2025-05-29 17:28:06 +08:00
|
|
|
|
|
|
|
translated_raw =
|
|
|
|
ContentSplitter
|
|
|
|
.split(post.raw)
|
2025-06-06 23:24:24 +08:00
|
|
|
.map do |text|
|
|
|
|
PostRawTranslator.new(
|
|
|
|
text:,
|
|
|
|
target_locale:,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_id: post.id,
|
|
|
|
).translate
|
|
|
|
end
|
2025-05-29 17:28:06 +08:00
|
|
|
.join("")
|
|
|
|
|
|
|
|
localization =
|
2025-06-06 23:24:24 +08:00
|
|
|
PostLocalization.find_or_initialize_by(post_id: post.id, locale: target_locale)
|
2025-05-29 17:28:06 +08:00
|
|
|
|
|
|
|
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
|