0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/lib/localized_cooked_post_processor.rb
Natalie Tay e4b67f4b32
FEATURE: Localize local oneboxes (#40493)
We currently see unlocalized oneboxes even when
`content_localization_enabled`. An internal topic onebox stored the
linked topic's title and excerpt in its original language, so a reader
using content localization saw it untranslated even when a translation
existed.

This shows internal topic oneboxes in the reader's language via two
purpose-built paths (no rewriting cooked HTML at request time):

- For translated posts, `LocalizedCookedPostProcessor` bakes the card in
the localization's locale at cook time; the original cooked is
untouched.
- For "original posts", the serializer adds a `localized_oneboxes` map
(built once per page from `topic_links`, scoped to onebox links + the
reader's locale family), and a post-cooked decorator swaps the
title/excerpt into the rendered card.

Falls back to the original when no translation exists, respects "show
original", and never exposes a title/preview the reader couldn't already
see. "Rebuild HTML" now refreshes localizations too, without
re-translating.
2026-06-08 11:49:55 +08:00

29 lines
803 B
Ruby
Vendored

# frozen_string_literal: true
class LocalizedCookedPostProcessor
include CookedProcessorMixin
def initialize(post_localization, post, opts = {})
@post_localization = post_localization
@post = post
@opts = (opts || {}).merge(locale: post_localization.locale)
@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?
@size_cache = {}
end
def post_process
post_process_oneboxes
post_process_images
@post_localization.link_post_uploads(fragments: @doc)
end
def html
@doc.try(:to_html)
end
end