mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 00:31:23 +08:00
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:
@ -22,6 +22,7 @@ end
|
||||
# localizer_user_id :integer not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# excerpt :string
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
@ -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
|
||||
|
@ -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
|
@ -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
|
||||
|
Reference in New Issue
Block a user