FEATURE: Also localize topic excerpts (#32839)

Currently, topic excerpts are not localized.

This commit adds the excerpt column into topic_localization and displays the
localized excerpt if present.
This commit is contained in:
Natalie Tay
2025-05-21 17:49:27 +08:00
committed by GitHub
parent b7fcc0e854
commit 94e653af08
4 changed files with 46 additions and 3 deletions

View File

@ -22,6 +22,7 @@ end
# localizer_user_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# excerpt :string
#
# Indexes
#

View File

@ -111,7 +111,13 @@ class ListableTopicSerializer < BasicTopicSerializer
end
def excerpt
object.excerpt
e = object.excerpt
if (ContentLocalization.show_translated_topic?(object, scope))
object.get_localization&.excerpt.presence || e
else
e
end
end
alias include_last_read_post_number? has_user_data

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddExcerptToTopicLocalization < ActiveRecord::Migration[7.2]
def change
add_column :topic_localizations, :excerpt, :string, null: true, default: nil
end
end

View File

@ -1,9 +1,12 @@
# frozen_string_literal: true
RSpec.describe ListableTopicSerializer do
describe ListableTopicSerializer do
fab!(:topic)
describe "#excerpt" do
it "can be extended by theme modifiers" do
before { topic.update!(excerpt: "This is excerrrpt-ional") }
it "can be included by theme modifiers" do
payload = TopicListItemSerializer.new(topic, scope: Guardian.new, root: false).as_json
expect(payload[:excerpt]).to eq(nil)
@ -24,5 +27,31 @@ RSpec.describe ListableTopicSerializer do
expect(payload[:excerpt]).to eq(topic.excerpt)
end
it "does not include the excerpt by default" do
json = ListableTopicSerializer.new(topic, scope: Guardian.new).as_json
expect(json[:listable_topic][:excerpt]).to eq(nil)
end
it "returns the topic's excerpt" do
SiteSetting.always_include_topic_excerpts = true
json = ListableTopicSerializer.new(topic, scope: Guardian.new).as_json
expect(json[:listable_topic][:excerpt]).to eq("This is excerrrpt-ional")
end
it "returns the localized excerpt when setting is enabled" do
I18n.locale = "ja"
topic.update!(locale: "en")
Fabricate(:topic_localization, topic:, excerpt: "X", locale: "ja")
SiteSetting.experimental_content_localization = true
SiteSetting.always_include_topic_excerpts = true
json = ListableTopicSerializer.new(topic, scope: Guardian.new).as_json
expect(json[:listable_topic][:excerpt]).to eq("X")
end
end
end