A platform for community discussion. Free, open, simple.
FIX: Navigating via topic filters should not untranslate a category name (#35211)
Natalie Tay8841ce71c1ed
diff --git a/app/serializers/category_badge_serializer.rb b/app/serializers/category_badge_serializer.rbindex 005a8e584ff..69fa095653f 100644--- a/app/serializers/category_badge_serializer.rb+++ b/app/serializers/category_badge_serializer.rb@@ -17,11 +17,16 @@ class CategoryBadgeSerializer < ApplicationSerializerenddef name- if object.uncategorized?- I18n.t("uncategorized_category_name", locale: SiteSetting.default_locale)- else- object.name- end+ return I18n.t("uncategorized_category_name") if object.uncategorized?++ translated =+ if (ContentLocalization.show_translated_category?(object, scope))+ object.get_localization&.name+ else+ object.name+ end++ translated || object.nameenddef description_textdiff --git a/spec/serializers/topic_list_serializer_spec.rb b/spec/serializers/topic_list_serializer_spec.rbindex ed0ad38eccd..da5213eccf0 100644--- a/spec/serializers/topic_list_serializer_spec.rb+++ b/spec/serializers/topic_list_serializer_spec.rb@@ -2,8 +2,10 @@RSpec.describe TopicListSerializer dofab!(:user)+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, user:, category:) }- let(:topic) { Fabricate(:topic).tap { |t| t.allowed_user_ids = [t.user_id] } }+ before { topic.allowed_user_ids = [topic.user_id] }it "should return the right payload" dotopic_list = TopicList.new(nil, user, [topic])@@ -23,4 +25,64 @@ RSpec.describe TopicListSerializer doexpect(serializer.options[:filter]).to eq(filter)end++ describe "has categories" do+ describe "when lazy loading categories enabled" do+ before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }++ it "serializes categories when lazy loading" do+ topic_list = TopicList.new(nil, user, [topic])+ guardian = Guardian.new(user)++ serialized = described_class.new(topic_list, scope: guardian).as_json++ expect(serialized[:topic_list][:categories].first[:id]).to eq(topic.category.id)+ end++ describe "content localization" do+ fab!(:category_localization) do+ Fabricate(:category_localization, category:, locale: "es", name: "Solicitudes")+ end++ describe "when enabled" do+ it "returns localized category name and description when locale param is passed" do+ SiteSetting.content_localization_enabled = true+ topic_list = TopicList.new(nil, user, [topic])+ guardian = Guardian.new(user)++ I18n.locale = "es"+ serialized =+ described_class.new(topic_list, scope: guardian, params: { locale: "es" }).as_json++ expect(serialized[:topic_list][:categories].first[:name]).to eq("Solicitudes")+ end+ end++ describe "when disabled" do+ it "returns default category name and description" do+ SiteSetting.content_localization_enabled = false+ topic_list = TopicList.new(nil, user, [topic])+ guardian = Guardian.new(user)++ I18n.locale = "es"+ serialized = described_class.new(topic_list, scope: guardian).as_json++ expect(serialized[:topic_list][:categories].first[:name]).to eq(category.name)+ end+ end+ end+ end++ describe "when lazy loading categories disabled" do+ it "does not serialize categories when not lazy loading" do+ SiteSetting.lazy_load_categories_groups = ""+ topic_list = TopicList.new(nil, user, [topic])+ guardian = Guardian.new(user)++ serialized = described_class.new(topic_list, scope: guardian).as_json++ expect(serialized[:topic_list]).not_to have_key(:categories)+ end+ end+ endenddiff --git a/spec/system/category_localizations_spec.rb b/spec/system/category_localizations_spec.rbindex f1f7b087976..4b3e656d948 100644--- a/spec/system/category_localizations_spec.rb+++ b/spec/system/category_localizations_spec.rb@@ -175,7 +175,7 @@ describe "Category Localizations", type: :system dobefore { SiteSetting.content_localization_language_switcher = "all" }- describe "for anonymous users" do+ shared_examples_for "navigating the site via various category links" doit "keeps the translated category name when navigating sidebar" dovisit("/")switcher.expand@@ -223,6 +223,30 @@ describe "Category Localizations", type: :system doexpect(topic_list.topic(cat_topic)).to have_text("Solicitudes")endend++ describe "for anonymous users" do+ it_behaves_like "navigating the site via various category links"+ end++ describe "logged in users" do+ describe "lazy loaded categories" do+ before do+ SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"+ sign_in(admin)+ end++ it_behaves_like "navigating the site via various category links"+ end++ describe "no lazy loaded categories" do+ before do+ SiteSetting.lazy_load_categories_groups = ""+ sign_in(admin)+ end++ it_behaves_like "navigating the site via various category links"+ end+ endendendend