0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 20:29:55 +08:00
discourse/lib/localization_attributes_replacer.rb
Kris ffcd045019
FIX: only show first paragraph of localized category descriptions (#41511)
Reported here:
https://meta.discourse.org/t/category-description-truncates-to-first-paragraph-for-primary-base-language-but-not-for-localizations-panel-languages/406801

In a number of places we only show the first paragraph of a category
description by default, but when someone manually adds a category
description translation we show the full content. This results in
translated content being much longer in some places where only a single
paragraph is expected (/categories pages, category headers, etc).

This change cooks the translated description and applies the same single
paragraph rule where relevant.

`Category.first_paragraph_description` has been extracted to be used in
both the default and translated descriptions so the behavior remains
consistent.
2026-07-10 16:59:40 -04:00

52 lines
1.8 KiB
Ruby
Vendored

# frozen_string_literal: true
module LocalizationAttributesReplacer
def self.localize_category(category, locale)
if loc = get_localization(category, locale)
category.name = loc.name if loc.name.present?
localized_description = loc.description_first_paragraph
category.description = localized_description if localized_description.present?
end
end
def self.replace_category_attributes(category, crawl_locale)
localize_category(category, crawl_locale)
while category = category.parent_category
localize_category(category, crawl_locale)
end
end
def self.replace_topic_attributes(topic, crawl_locale)
if loc = get_localization(topic, crawl_locale)
# assigning directly to title would commit the change to the database
# due to the setter method defined in the Topic model.
# fancy_title must also be set to prevent the lazy DB write in
# Topic#fancy_title from persisting a localized value when fancy_title is NULL.
if loc.title.present?
topic.send(:write_attribute, :title, loc.title)
topic.send(
:write_attribute,
:fancy_title,
loc.fancy_title.presence || Topic.fancy_title(loc.title),
)
end
topic.excerpt = loc.excerpt if loc.excerpt.present?
end
replace_category_attributes(topic.category, crawl_locale) if topic&.category.present?
end
def self.replace_post_attributes(post, crawl_locale)
if loc = get_localization(post, crawl_locale)
post.cooked = loc.cooked if loc.cooked.present?
end
end
private
def self.get_localization(model, crawl_locale)
model.present? && model.locale.present? &&
!LocaleNormalizer.is_same?(model.locale, crawl_locale) && model.get_localization(crawl_locale)
end
end