0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 12:09:27 +08:00
discourse/app/jobs/regular/process_localized_cooked.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

39 lines
1.4 KiB
Ruby
Vendored

# frozen_string_literal: true
module Jobs
class ProcessLocalizedCooked < ::Jobs::Base
def execute(args)
DistributedMutex.synchronize(
"process_localized_cook_#{args[:post_localization_id]}",
validity: 10.minutes,
) do
post_localization = PostLocalization.find_by(id: args[:post_localization_id])
return if post_localization.blank?
post = post_localization.post
return if post.blank? || post.topic.blank?
# On rebake the stored cooked already holds rendered onebox cards, so
# re-cook from raw first to restore the a.onebox links the processor
# re-renders (now in the reader's locale). No content is re-translated.
if args[:recook]
recooked = post.post_analyzer.cook(post_localization.raw, post.cooking_options || {})
post_localization.update_column(:cooked, recooked) if recooked.present?
end
processor = LocalizedCookedPostProcessor.new(post_localization, post, {})
processor.post_process
cooked = processor.html.strip
post_localization.update_column(:cooked, cooked) if cooked.present?
if post.is_first_post?
topic_localization = post.topic.localizations.find_by(locale: post_localization.locale)
topic_localization.update_excerpt(cooked:) if topic_localization
end
MessageBus.publish("/topic/#{post.topic_id}", type: :localized, id: post.id)
end
end
end
end