2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-03-03 23:54:20 +08:00
discourse/app/serializers/site_category_serializer.rb
Natalie Tay 6bde8bacd7
FIX: Also ensure CategoryBoxes that use description_excerpt have them localized (#37924)
When accessing a category on the frontend via Category.findById() (e.g.
in theme components, sidebar, category dropdowns), the data comes from
/site.json which is serialized by SiteCategorySerializer.

SiteCategorySerializer already localizes name and description, but
description_text and description_excerpt (inherited from
BasicCategorySerializer) always return values from the original model,
bypassing localization.

This means any UI (e.g. `<CategoryBoxes>`) that reads
`description_excerpt` global category store (rather than from
/categories.json, which localizes at the query level via CategoryList)
will show non-localized descriptions even when the user's locale differs
from the category's locale.

This change adds `description_text` and `description_excerpt` overrides
to `SiteCategorySerializer` that use the localized description when
available.
  
  ### before
  
(this is a custom theme component that's added to `/latest` to repro,
this is not `/categories`)
  
<img width="1140" height="369" alt="Screenshot 2026-02-19 at 9 51 13 PM"
src="https://github.com/user-attachments/assets/d1832486-dc73-45dc-be71-32306b6e0412"
/>

### after

<img width="1140" height="369" alt="Screenshot 2026-02-19 at 9 51 39 PM"
src="https://github.com/user-attachments/assets/81798d2d-7c40-44cb-afa9-c0c3dba83787"
/>
2026-02-19 22:47:57 +08:00

78 lines
1.9 KiB
Ruby

# frozen_string_literal: true
class SiteCategorySerializer < BasicCategorySerializer
attributes :allowed_tags,
:allowed_tag_groups,
:allow_global_tags,
:read_only_banner,
:form_template_ids
has_many :category_required_tag_groups, key: :required_tag_groups, embed: :objects
def form_template_ids
object.form_template_ids.sort
end
def include_allowed_tags?
SiteSetting.tagging_enabled
end
def allowed_tags
object.tags.pluck(:id, :name, :slug).map { |id, name, slug| { id: id, name: name, slug: slug } }
end
def include_allowed_tag_groups?
SiteSetting.tagging_enabled
end
def allowed_tag_groups
object.tag_groups.pluck(:name)
end
def include_allow_global_tags?
SiteSetting.tagging_enabled
end
def include_required_tag_groups?
SiteSetting.tagging_enabled
end
def name
return I18n.t("uncategorized_category_name") if object.uncategorized?
translated_name =
if (ContentLocalization.show_translated_category?(object, scope))
object.get_localization&.name
else
object.name
end
translated_name || object.name
end
def description
localized_description || object.description
end
def description_text
return super if object.uncategorized?
return ERB::Util.html_escape(localized_description).html_safe if localized_description
object.description_text
end
def description_excerpt
return super if object.uncategorized?
return PrettyText.excerpt(localized_description, 300) if localized_description
object.description_excerpt
end
private
def localized_description
return @localized_description if defined?(@localized_description)
@localized_description =
if ContentLocalization.show_translated_category?(object, scope)
object.get_localization&.description.presence
end
end
end