0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/lib/content_localization/onebox_localizer_spec.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

55 lines
1.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe ContentLocalization::OneboxLocalizer do
fab!(:reader) { Fabricate(:user, locale: "ja") }
fab!(:source_topic, :topic)
fab!(:source_post) do
Fabricate(:post, topic: source_topic, post_number: 1, locale: "ja", raw: "見てください")
end
before { SiteSetting.content_localization_enabled = true }
def add_localized_onebox(index)
topic = Fabricate(:topic, title: "Linked topic number #{index} goes here", locale: "en")
post = Fabricate(:post, topic: topic, post_number: 1, locale: "en", raw: "body number #{index}")
Fabricate(:topic_localization, topic: topic, locale: "ja", title: "翻訳タイトル#{index}")
Fabricate(:post_localization, post: post, locale: "ja", cooked: "<p>翻訳本文#{index}</p>")
TopicLink.create!(
topic: source_topic,
post: source_post,
user: source_post.user,
url: post.url,
domain: Discourse.current_hostname,
internal: true,
quote: true,
reflection: false,
link_topic_id: topic.id,
link_post_id: post.id,
)
end
def build
I18n.with_locale(:ja) do
described_class.build(
posts: [source_post],
guardian: Guardian.new(reader),
category: source_topic.category,
locale: :ja,
)
end
end
it "batches its lookups into one query each regardless of onebox count (no N+1)" do
add_localized_onebox(1)
add_localized_onebox(2)
add_localized_onebox(3)
queries = track_sql_queries { build }
expect(queries.count { |q| q =~ /FROM "?topic_links"?/ }).to eq(1)
expect(queries.count { |q| q =~ /FROM "?topics"?/ }).to eq(1)
expect(queries.count { |q| q =~ /FROM "?posts"?/ }).to eq(1)
expect(queries.count { |q| q =~ /FROM "?topic_localizations"?/ }).to eq(1)
expect(queries.count { |q| q =~ /FROM "?post_localizations"?/ }).to eq(1)
end
end