Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Also localize topic excerpts (#32839)

Natalie Tay94e653af0866
diff --git a/app/models/topic_localization.rb b/app/models/topic_localization.rb
index 5377fc6a2cf..3429886d67f 100644
--- a/app/models/topic_localization.rb
+++ b/app/models/topic_localization.rb
@@ -22,6 +22,7 @@ end
# localizer_user_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
+# excerpt :string
#
# Indexes
#
diff --git a/app/serializers/listable_topic_serializer.rb b/app/serializers/listable_topic_serializer.rb
index 8dfd5fb3d79..9e1db59ea51 100644
--- a/app/serializers/listable_topic_serializer.rb
+++ b/app/serializers/listable_topic_serializer.rb
@@ -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
diff --git a/db/migrate/20250521053324_add_excerpt_to_topic_localization.rb b/db/migrate/20250521053324_add_excerpt_to_topic_localization.rb
new file mode 100644
index 00000000000..35fb19db37a
--- /dev/null
+++ b/db/migrate/20250521053324_add_excerpt_to_topic_localization.rb
@@ -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
diff --git a/spec/serializers/listable_topic_serializer_spec.rb b/spec/serializers/listable_topic_serializer_spec.rb
index 92d3edb050b..31e63f78e42 100644
--- a/spec/serializers/listable_topic_serializer_spec.rb
+++ b/spec/serializers/listable_topic_serializer_spec.rb
@@ -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