discourse/lib/topic_localization_creator.rb
Natalie Tay 73a227089a
FIX: Update topic excerpts when the first post is localized (#36885)
When localizing a pinned topic, the excerpt shown in the topic list
wasn't updating - only the title would change when switching languages.
This was because the `TopicLocalization.excerpt` field was not being
updated.

The excerpt needs to be set in two scenarios depending on which gets
created first:
- When a post localization is created/updated for the first post, update
the topic localization's excerpt
- When a topic localization is created and a post localization already
exists, populate the excerpt from it

Meta:
https://meta.discourse.org/t/localizing-topic-does-not-seem-to-localize-excerpt-pinned-on-topic-list/392232
2025-12-29 23:33:39 +08:00

31 lines
776 B
Ruby

# frozen_string_literal: true
class TopicLocalizationCreator
def self.create(topic:, locale:, title:, user:)
Guardian.new(user).ensure_can_localize_topic!(topic)
excerpt = nil
first_post = topic.first_post
if first_post
post_localization = first_post.localizations.find_by(locale:)
if post_localization
excerpt =
Post.excerpt(
post_localization.cooked,
SiteSetting.topic_excerpt_maxlength,
strip_links: true,
strip_images: true,
)
end
end
TopicLocalization.create!(
topic_id: topic.id,
locale: locale,
title: title,
fancy_title: Topic.fancy_title(title),
localizer_user_id: user.id,
excerpt: excerpt,
)
end
end