2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00

FEATURE: Add oneboxes to localized posts

This change ensures that oneboxes are correctly generated for localized posts.

The `PostLocalizer` was using `PrettyText.cook` directly, which does not perform all the necessary post-processing steps, such as onebox generation.

This commit introduces a new `LocalizedCookedPostProcessor` class that is responsible for post-processing the cooked HTML of translated posts. This new class reuses the `CookedProcessorMixin` to gain access to the `post_process_oneboxes` method.

The `PostLocalizer` is updated to use this new processor, ensuring that oneboxes are correctly generated in the translated content.
This commit is contained in:
Rafael Silva 2025-09-22 13:25:23 -03:00
parent b8e86ceb23
commit e46fe3af1e
No known key found for this signature in database
3 changed files with 58 additions and 1 deletions

View file

@ -0,0 +1,30 @@
# frozen_string_literal: true

module DiscourseAi
module Translation
class LocalizedCookedPostProcessor
include CookedProcessorMixin

def initialize(post_localization, post, opts = {})
@post_localization = post_localization
@post = post
@opts = opts
@doc = Loofah.html5_fragment(@post_localization.cooked)
@cooking_options = @post.cooking_options || {}
@cooking_options[:topic_id] = @post.topic_id
@cooking_options = @cooking_options.symbolize_keys
@model = @post
@category_id = @post&.topic&.category_id
@omit_nofollow = @post.omit_nofollow?
end

def post_process
post_process_oneboxes
end

def html
@doc.try(:to_html)
end
end
end
end

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true

require_relative "localized_cooked_post_processor"

module DiscourseAi
module Translation
class PostLocalizer
@ -17,7 +19,12 @@ module DiscourseAi
PostLocalization.find_or_initialize_by(post_id: post.id, locale: target_locale)

localization.raw = translated_raw
localization.cooked = PrettyText.cook(translated_raw)
localization.cooked = post.post_analyzer.cook(translated_raw, post.cooking_options || {})

cooked_processor = LocalizedCookedPostProcessor.new(localization, post, {})
cooked_processor.post_process
localization.cooked = cooked_processor.html

localization.post_version = post.version
localization.localizer_user_id = Discourse.system_user.id
localization.save!

View file

@ -87,6 +87,26 @@ describe DiscourseAi::Translation::PostLocalizer do
expect(out.cooked).to eq(cooked)
}.to_not change { PostLocalization.count }
end

context "with oneboxes" do
fab!(:topic_to_onebox) { Fabricate(:topic) }
let(:onebox_url) { topic_to_onebox.url }
let(:post) { Fabricate(:post, raw: onebox_url) }
let(:translated_raw) { onebox_url }
let(:onebox_html) do
"<aside class=\"onebox\"><a href=\"#{onebox_url}\">#{topic_to_onebox.title}</a></aside>"
end

before { Oneboxer.stubs(:onebox).with(onebox_url, anything).returns(onebox_html) }

it "creates oneboxes in the cooked HTML" do
post_raw_translator_stub(
{ text: post.raw, target_locale: "ja", translated: translated_raw },
)
localization = described_class.localize(post, "ja")
expect(localization.cooked).to include(onebox_html)
end
end
end

describe ".has_relocalize_quota?" do