Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Show localized tags (#37044)

Natalie Taya349b9a6c178
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 98d321bc61e..42f6860c8b7 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -49,17 +49,22 @@ class TagsController < ::ApplicationController
@title = @description_meta
show_all_tags = guardian.can_admin_tags? && guardian.is_admin?
+ preload_localizations = SiteSetting.content_localization_enabled
if SiteSetting.tags_listed_by_group
ungrouped_tags = Tag.where("tags.id NOT IN (SELECT tag_id FROM tag_group_memberships)")
ungrouped_tags = ungrouped_tags.used_tags_in_regular_topics(guardian) unless show_all_tags
ungrouped_tags = ungrouped_tags.order(:id)
+ ungrouped_tags = ungrouped_tags.includes(:localizations) if preload_localizations
+
+ tag_group_includes =
+ preload_localizations ? { none_synonym_tags: :localizations } : :none_synonym_tags
grouped_tag_counts =
TagGroup
.visible(guardian)
.order("name ASC")
- .includes(:none_synonym_tags)
+ .includes(tag_group_includes)
.map do |tag_group|
{
id: tag_group.id,
@@ -74,6 +79,10 @@ class TagsController < ::ApplicationController
tags = show_all_tags ? Tag.all : Tag.used_tags_in_regular_topics(guardian)
tags = tags.order(:id)
unrestricted_tags = DiscourseTagging.filter_visible(tags.where(target_tag_id: nil), guardian)
+ unrestricted_tags = unrestricted_tags.includes(:localizations) if preload_localizations
+
+ category_includes =
+ preload_localizations ? { none_synonym_tags: :localizations } : :none_synonym_tags
categories =
Category
@@ -81,7 +90,7 @@ class TagsController < ::ApplicationController
"id IN (SELECT category_id FROM category_tags WHERE category_id IN (?))",
guardian.allowed_category_ids,
)
- .includes(:none_synonym_tags)
+ .includes(category_includes)
.order(:id)
category_tag_counts =
@@ -135,6 +144,7 @@ class TagsController < ::ApplicationController
tags_count = tags.count
tags = tags.order("LOWER(tags.name) ASC").limit(LIST_LIMIT).offset(offset * LIST_LIMIT)
+ tags = tags.includes(:localizations) if SiteSetting.content_localization_enabled
load_more_url = URI("/tags/list.json")
load_more_url.query = URI.encode_www_form(load_more_query_params)
@@ -380,6 +390,13 @@ class TagsController < ::ApplicationController
tags_with_counts, filter_result_context =
DiscourseTagging.filter_allowed_tags(guardian, **filter_params, with_context: true)
+ if SiteSetting.content_localization_enabled && tags_with_counts.present?
+ ActiveRecord::Associations::Preloader.new(
+ records: tags_with_counts,
+ associations: :localizations,
+ ).call
+ end
+
tags = self.class.tag_counts_json(tags_with_counts, guardian)
json_response = { results: tags }
@@ -577,12 +594,21 @@ class TagsController < ::ApplicationController
next if topic_count == 0 && t.pm_topic_count > 0 && !show_pm_tags
+ tag_name = t.name
+ tag_description = t.description
+
+ if ContentLocalization.show_translated_tag?(t, guardian)
+ localization = t.get_localization
+ tag_name = localization&.name || tag_name
+ tag_description = localization&.description || tag_description
+ end
+
attrs = {
id: t.id,
- text: t.name,
- name: t.name,
+ text: tag_name,
+ name: tag_name,
slug: t.slug.presence || "#{t.id}-tag",
- description: t.description,
+ description: tag_description,
count: topic_count,
pm_only: topic_count == 0 && t.pm_topic_count > 0,
target_tag:
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 3afdb970fa9..74f073f5077 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -153,7 +153,7 @@ class Tag < ActiveRecord::Base
end
)
- tag_names_with_counts = DB.query <<~SQL
+ tag_data = DB.query <<~SQL
SELECT tags.id as tag_id, tags.name as tag_name, tags.slug as tag_slug, SUM(stats.topic_count) AS sum_topic_count
FROM category_tag_stats stats
JOIN tags ON stats.tag_id = tags.id AND stats.topic_count > 0
@@ -164,9 +164,27 @@ class Tag < ActiveRecord::Base
LIMIT #{limit}
SQL
- tag_names_with_counts.map do |row|
+ return [] if tag_data.empty?
+
+ unless SiteSetting.content_localization_enabled
+ return(
+ tag_data.map do |row|
+ slug = row.tag_slug.presence || "#{row.tag_id}-tag"
+ { id: row.tag_id, name: row.tag_name, slug: }
+ end
+ )
+ end
+
+ tags_by_id = Tag.where(id: tag_data.map(&:tag_id)).includes(:localizations).index_by(&:id)
+ show_localized = !ContentLocalization.show_original?(guardian)
+
+ tag_data.filter_map do |row|
+ tag = tags_by_id[row.tag_id]
+ next unless tag
+
+ name = show_localized ? (tag.get_localization&.name || tag.name) : tag.name
slug = row.tag_slug.presence || "#{row.tag_id}-tag"
- { id: row.tag_id, name: row.tag_name, slug: slug }
+ { id: tag.id, name:, slug: }
end
end
diff --git a/app/serializers/concerns/navigation_menu_tags_mixin.rb b/app/serializers/concerns/navigation_menu_tags_mixin.rb
index 987cb2bdf18..e7f7a10519a 100644
--- a/app/serializers/concerns/navigation_menu_tags_mixin.rb
+++ b/app/serializers/concerns/navigation_menu_tags_mixin.rb
@@ -4,8 +4,15 @@ module NavigationMenuTagsMixin
def serialize_tags(tags)
topic_count_column = Tag.topic_count_column(scope)
- tags
- .order(topic_count_column => :desc)
- .map { |tag| SidebarTagSerializer.new(tag, scope: scope, root: false).as_json }
+ ordered_tags = tags.order(topic_count_column => :desc).to_a
+
+ if SiteSetting.content_localization_enabled
+ ActiveRecord::Associations::Preloader.new(
+ records: ordered_tags,
+ associations: :localizations,
+ ).call
+ end
+
+ ordered_tags.map { |tag| SidebarTagSerializer.new(tag, scope: scope, root: false).as_json }
end
end
diff --git a/app/serializers/concerns/topic_tags_mixin.rb b/app/serializers/concerns/topic_tags_mixin.rb
index 3ff0ff2bdd9..e96f143db9c 100644
--- a/app/serializers/concerns/topic_tags_mixin.rb
+++ b/app/serializers/concerns/topic_tags_mixin.rb
@@ -13,13 +13,15 @@ module TopicTagsMixin
end
def tags
- all_tags.map { |tag| { id: tag.id, name: tag.name, slug: tag.slug_for_url } }
+ all_tags.map { |tag| { id: tag.id, name: localized_tag_name(tag), slug: tag.slug_for_url } }
end
def tags_descriptions
all_tags
.each
- .with_object({}) { |tag, acc| acc[tag.name] = tag.description&.truncate(DESCRIPTION_LIMIT) }
+ .with_object({}) do |tag, acc|
+ acc[localized_tag_name(tag)] = localized_tag_description(tag)&.truncate(DESCRIPTION_LIMIT)
+ end
.compact
end
@@ -29,6 +31,22 @@ module TopicTagsMixin
private
+ def localized_tag_name(tag)
+ if ContentLocalization.show_translated_tag?(tag, scope)
+ tag.get_localization&.name || tag.name
+ else
+ tag.name
+ end
+ end
+
+ def localized_tag_description(tag)
+ if ContentLocalization.show_translated_tag?(tag, scope)
+ tag.get_localization&.description || tag.description
+ else
+ tag.description
+ end
+ end
+
def all_tags
return @tags if defined?(@tags)
diff --git a/app/serializers/sidebar_tag_serializer.rb b/app/serializers/sidebar_tag_serializer.rb
index 99a71396bfc..08e31145125 100644
--- a/app/serializers/sidebar_tag_serializer.rb
+++ b/app/serializers/sidebar_tag_serializer.rb
@@ -7,6 +7,20 @@ class SidebarTagSerializer < ApplicationSerializer
object.slug_for_url
end
+ def name
+ translated =
+ (object.get_localization&.name if ContentLocalization.show_translated_tag?(object, scope))
+ translated || object.name
+ end
+
+ def description
+ translated =
+ if ContentLocalization.show_translated_tag?(object, scope)
+ object.get_localization&.description
+ end
+ translated || object.description
+ end
+
def pm_only
topic_count_column = Tag.topic_count_column(scope)
object.public_send(topic_count_column) == 0 && object.pm_topic_count > 0
diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb
index 48545e0cb9d..e628a72759f 100644
--- a/app/serializers/site_serializer.rb
+++ b/app/serializers/site_serializer.rb
@@ -317,11 +317,11 @@ class SiteSerializer < ApplicationSerializer
def navigation_menu_site_top_tags
if top_tags.present?
top_tag_objects = top_tags[0...SIDEBAR_TOP_TAGS_TO_SHOW]
- tag_names = top_tag_objects.map { |t| t[:name] }
- serialized = serialize_tags(Tag.where(name: tag_names))
+ tag_ids = top_tag_objects.map { |t| t[:id] }
+ serialized = serialize_tags(Tag.where(id: tag_ids))
# Ensures order of top tags is preserved
- serialized.sort_by { |tag| tag_names.index(tag[:name]) }
+ serialized.sort_by { |tag| tag_ids.index(tag[:id]) }
else
[]
end
diff --git a/app/serializers/tag_serializer.rb b/app/serializers/tag_serializer.rb
index 71d0c75681d..89e5409f9a5 100644
--- a/app/serializers/tag_serializer.rb
+++ b/app/serializers/tag_serializer.rb
@@ -9,12 +9,26 @@ class TagSerializer < ApplicationSerializer
object.slug_for_url
end
+ def name
+ translated =
+ (object.get_localization&.name if ContentLocalization.show_translated_tag?(object, scope))
+ translated || object.name
+ end
+
+ def description
+ translated =
+ if ContentLocalization.show_translated_tag?(object, scope)
+ object.get_localization&.description
+ end
+ translated || object.description
+ end
+
def topic_count
object.public_send(Tag.topic_count_column(scope))
end
def staff
- DiscourseTagging.staff_tag_names.include?(name)
+ DiscourseTagging.staff_tag_names.include?(object.name)
end
def include_localizations?
diff --git a/lib/content_localization.rb b/lib/content_localization.rb
index bfb1372ab46..9ce2a036761 100644
--- a/lib/content_localization.rb
+++ b/lib/content_localization.rb
@@ -28,10 +28,20 @@ class ContentLocalization
end
# This method returns true when we should try to show the translated category.
- # @param scope [Object] The serializer scope from which the method is called
# @param category [Category] The category record
+ # @param scope [Object] The serializer scope from which the method is called
# @return [Boolean]
def self.show_translated_category?(category, scope)
- SiteSetting.content_localization_enabled && !category.in_user_locale? && !show_original?(scope)
+ SiteSetting.content_localization_enabled && category.locale.present? &&
+ !category.in_user_locale? && !show_original?(scope)
+ end
+
+ # This method returns true when we should try to show the translated tag.
+ # @param tag [Tag] The tag record
+ # @param scope [Object] The serializer scope from which the method is called
+ # @return [Boolean]
+ def self.show_translated_tag?(tag, scope)
+ SiteSetting.content_localization_enabled && tag.locale.present? && !tag.in_user_locale? &&
+ !show_original?(scope)
end
end
diff --git a/lib/topic_query.rb b/lib/topic_query.rb
index 696a03b9755..e06cc7e24f6 100644
--- a/lib/topic_query.rb
+++ b/lib/topic_query.rb
@@ -862,7 +862,12 @@ class TopicQuery
if SiteSetting.tagging_enabled
# Use `preload` here instead since `includes` can end up calling `eager_load` which can unnecessarily lead to
# joins on the `topic_tags` and `tags` table leading to a much slower query.
- result = result.preload(:tags)
+ result =
+ if SiteSetting.content_localization_enabled
+ result.preload(tags: :localizations)
+ else
+ result.preload(:tags)
+ end
result = filter_by_tags(result)
end
@@ -1259,7 +1264,14 @@ class TopicQuery
"LEFT JOIN topic_users tu ON topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i}",
)
- query = query.includes(:tags) if SiteSetting.tagging_enabled
+ if SiteSetting.tagging_enabled
+ query =
+ if SiteSetting.content_localization_enabled
+ query.includes(tags: :localizations)
+ else
+ query.includes(:tags)
+ end
+ end
query.order("topics.bumped_at DESC")
end
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 8151f465f06..a38b09b523a 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -946,7 +946,13 @@ class TopicView
def find_topic(topic_or_topic_id)
return topic_or_topic_id if topic_or_topic_id.is_a?(Topic)
# with_deleted covered in #check_and_raise_exceptions
- Topic.with_deleted.includes(:category).find_by(id: topic_or_topic_id)
+ tags_include =
+ if SiteSetting.tagging_enabled && SiteSetting.content_localization_enabled
+ { tags: :localizations }
+ elsif SiteSetting.tagging_enabled
+ :tags
+ end
+ Topic.with_deleted.includes(:category, tags_include).find_by(id: topic_or_topic_id)
end
def find_post_replies_ids(post_id)
diff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rb
index 26a6582110e..3f9a867ef8a 100644
--- a/spec/lib/content_localization_spec.rb
+++ b/spec/lib/content_localization_spec.rb
@@ -152,5 +152,62 @@ describe ContentLocalization do
expect(ContentLocalization.show_translated_category?(category, scope)).to be false
end
+
+ it "returns false when category locale is nil" do
+ SiteSetting.content_localization_enabled = true
+ category.update!(locale: nil)
+ I18n.locale = "de"
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_category?(category, scope)).to be false
+ end
+ end
+
+ describe ".show_translated_tag?" do
+ fab!(:tag)
+
+ it "returns true when criteria met" do
+ SiteSetting.content_localization_enabled = true
+ tag.update!(locale: "ja")
+ I18n.locale = "de"
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_tag?(tag, scope)).to be true
+ end
+
+ context "when criteria not met" do
+ before do
+ SiteSetting.content_localization_enabled = true
+ tag.update!(locale: "ja")
+ I18n.locale = "de"
+ end
+
+ it "returns false when content_localization_enabled is false" do
+ SiteSetting.content_localization_enabled = false
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_tag?(tag, scope)).to be false
+ end
+
+ it "returns false when tag locale is nil" do
+ tag.update!(locale: nil)
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_tag?(tag, scope)).to be false
+ end
+
+ it "returns false when tag is in user locale" do
+ tag.update!(locale: I18n.locale)
+ scope = create_scope
+
+ expect(ContentLocalization.show_translated_tag?(tag, scope)).to be false
+ end
+
+ it "returns false when show_original? is true" do
+ scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
+
+ expect(ContentLocalization.show_translated_tag?(tag, scope)).to be false
+ end
+ end
end
end
diff --git a/spec/models/site_spec.rb b/spec/models/site_spec.rb
index 77c920c7001..3d2fee004b6 100644
--- a/spec/models/site_spec.rb
+++ b/spec/models/site_spec.rb
@@ -361,8 +361,8 @@ RSpec.describe Site do
it "returns localized category names when enabled" do
SiteSetting.content_localization_enabled = true
- localization = Fabricate(:category_localization)
- category = localization.category
+ category = Fabricate(:category, locale: "en")
+ localization = Fabricate(:category_localization, category:)
locale = localization.locale.to_sym
I18n.locale = locale
@@ -438,6 +438,24 @@ RSpec.describe Site do
expect(json_en).not_to eq(json_ja)
end
+
+ it "returns localized tag names for anonymous users based on locale" do
+ SiteSetting.tagging_enabled = true
+ tag = Fabricate(:tag, name: "cats", locale: "en")
+ Fabricate(:tag_localization, tag:, locale: "ja", name: "猫")
+ Fabricate(:topic, tags: [tag])
+
+ I18n.locale = :en
+ Discourse.redis.flushdb
+ json_en = Site.json_for(anon_guardian)
+
+ I18n.locale = :ja
+ json_ja = Site.json_for(anon_guardian)
+
+ expect(json_en).to include("cats")
+ expect(json_en).not_to include("猫")
+ expect(json_ja).to include("猫")
+ end
end
end
end
diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb
index 843ce98df56..82157ede056 100644
--- a/spec/models/tag_spec.rb
+++ b/spec/models/tag_spec.rb
@@ -217,6 +217,50 @@ RSpec.describe Tag do
expect(entry[:slug]).to eq("#{numeric_tag.id}-tag")
end
end
+
+ context "with localization" do
+ let(:localized_tag) { Fabricate(:tag, name: "cats", locale: "en") }
+ let!(:localization) do
+ Fabricate(:tag_localization, tag: localized_tag, locale: "ja", name: "猫")
+ end
+ let!(:tagged_topic) { Fabricate(:topic, tags: [localized_tag]) }
+
+ it "returns original names when localization disabled" do
+ SiteSetting.content_localization_enabled = false
+ I18n.locale = "ja"
+
+ expect(Tag.top_tags).to include(
+ { id: localized_tag.id, name: "cats", slug: localized_tag.slug },
+ )
+ end
+
+ it "returns localized names when localization enabled and user not in tag locale" do
+ SiteSetting.content_localization_enabled = true
+ I18n.locale = "ja"
+
+ expect(Tag.top_tags).to include(
+ { id: localized_tag.id, name: "猫", slug: localized_tag.slug },
+ )
+ end
+
+ it "returns original name when tag is in user locale" do
+ SiteSetting.content_localization_enabled = true
+ I18n.locale = "en"
+
+ expect(Tag.top_tags).to include(
+ { id: localized_tag.id, name: "cats", slug: localized_tag.slug },
+ )
+ end
+
+ it "falls back to original name when no localization exists for user's locale" do
+ SiteSetting.content_localization_enabled = true
+ I18n.locale = "de"
+
+ expect(Tag.top_tags).to include(
+ { id: localized_tag.id, name: "cats", slug: localized_tag.slug },
+ )
+ end
+ end
end
describe "#pm_tags" do
diff --git a/spec/serializers/tag_serializer_spec.rb b/spec/serializers/tag_serializer_spec.rb
index 60df05ef934..853e59360e9 100644
--- a/spec/serializers/tag_serializer_spec.rb
+++ b/spec/serializers/tag_serializer_spec.rb
@@ -37,4 +37,80 @@ RSpec.describe TagSerializer do
expect(serialized[:topic_count]).to eq(2)
end
end
+
+ describe "localization" do
+ fab!(:localized_tag, :tag) { Fabricate(:tag, name: "cats", locale: "en") }
+ fab!(:localization) do
+ Fabricate(
+ :tag_localization,
+ tag: localized_tag,
+ locale: "ja",
+ name: "猫",
+ description: "猫についてのタグです",
+ )
+ end
+
+ def serialize(tag)
+ described_class.new(tag, scope: Guardian.new(user), root: false).as_json
+ end
+
+ describe "#name" do
+ it "returns localized name when conditions met" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: "en")
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:name]).to eq("猫")
+ end
+
+ it "returns original name when localization disabled" do
+ SiteSetting.content_localization_enabled = false
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:name]).to eq("cats")
+ end
+
+ it "returns original name when tag has no locale" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: nil)
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:name]).to eq("cats")
+ end
+
+ it "returns original name when tag is in user locale" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: "ja")
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:name]).to eq("cats")
+ end
+ end
+
+ describe "#description" do
+ it "returns localized description when conditions met" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: "en", description: "A tag about cats")
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:description]).to eq("猫についてのタグです")
+ end
+
+ it "returns original description when localization disabled" do
+ SiteSetting.content_localization_enabled = false
+ localized_tag.update!(description: "A tag about cats")
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:description]).to eq("A tag about cats")
+ end
+
+ it "returns original description when tag has no locale" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: nil, description: "A tag about cats")
+ I18n.locale = "ja"
+
+ expect(serialize(localized_tag)[:description]).to eq("A tag about cats")
+ end
+ end
+ end
end
diff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rb
index 0edb4b6ee60..ec60d1a35d3 100644
--- a/spec/serializers/topic_list_item_serializer_spec.rb
+++ b/spec/serializers/topic_list_item_serializer_spec.rb
@@ -226,6 +226,78 @@ RSpec.describe TopicListItemSerializer do
end
end
+ describe "tag localization" do
+ fab!(:user)
+ fab!(:localized_tag, :tag) { Fabricate(:tag, name: "cats", locale: "en") }
+ fab!(:localization) do
+ Fabricate(
+ :tag_localization,
+ tag: localized_tag,
+ locale: "ja",
+ name: "猫",
+ description: "猫についてのタグです",
+ )
+ end
+
+ before do
+ SiteSetting.tagging_enabled = true
+ topic.tags << localized_tag
+ end
+
+ def serialize
+ TopicListItemSerializer.new(topic, scope: Guardian.new(user), root: false).as_json
+ end
+
+ it "returns localized tag name when conditions met" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: "en")
+ I18n.locale = "ja"
+
+ expect(serialize[:tags]).to include(
+ { id: localized_tag.id, name: "猫", slug: localized_tag.slug },
+ )
+ end
+
+ it "returns original tag name when localization disabled" do
+ SiteSetting.content_localization_enabled = false
+ I18n.locale = "ja"
+
+ expect(serialize[:tags]).to include(
+ { id: localized_tag.id, name: "cats", slug: localized_tag.slug },
+ )
+ end
+
+ it "returns original tag name when tag has no locale" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: nil)
+ I18n.locale = "ja"
+
+ expect(serialize[:tags]).to include(
+ { id: localized_tag.id, name: "cats", slug: localized_tag.slug },
+ )
+ end
+
+ it "uses localized tag name as key in tags_descriptions" do
+ SiteSetting.content_localization_enabled = true
+ localized_tag.update!(locale: "en", description: "A tag about cats")
+ I18n.locale = "ja"
+
+ expect(serialize[:tags_descriptions]).to have_key("猫")
+ expect(serialize[:tags_descriptions]).not_to have_key("cats")
+ expect(serialize[:tags_descriptions]["猫"]).to eq("猫についてのタグです")
+ end
+
+ it "uses original tag name as key when localization disabled" do
+ SiteSetting.content_localization_enabled = false
+ localized_tag.update!(description: "A tag about cats")
+ I18n.locale = "ja"
+
+ expect(serialize[:tags_descriptions]).to have_key("cats")
+ expect(serialize[:tags_descriptions]).not_to have_key("猫")
+ expect(serialize[:tags_descriptions]["cats"]).to eq("A tag about cats")
+ end
+ end
+
describe "#is_hot" do
describe "including the attr based on theme modifier or plugin registry" do
fab!(:hot_topic, :topic)
diff --git a/spec/serializers/topic_list_serializer_spec.rb b/spec/serializers/topic_list_serializer_spec.rb
index da5213eccf0..5b382839eb4 100644
--- a/spec/serializers/topic_list_serializer_spec.rb
+++ b/spec/serializers/topic_list_serializer_spec.rb
@@ -44,6 +44,8 @@ RSpec.describe TopicListSerializer do
Fabricate(:category_localization, category:, locale: "es", name: "Solicitudes")
end
+ before { category.update!(locale: "en") }
+
describe "when enabled" do
it "returns localized category name and description when locale param is passed" do
SiteSetting.content_localization_enabled = true
diff --git a/spec/system/category_localizations_spec.rb b/spec/system/category_localizations_spec.rb
index edf9d2d91cc..09801c461b0 100644
--- a/spec/system/category_localizations_spec.rb
+++ b/spec/system/category_localizations_spec.rb
@@ -12,6 +12,7 @@ describe "Category Localizations", type: :system do
topic_count: 1234,
subcategory_list_style: "boxes",
show_subcategory_list: true,
+ locale: "en",
)
end
let(:category_page) { PageObjects::Pages::Category.new }
@@ -159,6 +160,7 @@ describe "Category Localizations", type: :system do
name: "Subcategory",
description: "A",
parent_category: category,
+ locale: "en",
).tap do |category|
Fabricate(
:category_localization,
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 6f6298bc9f4..373440771eb 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -485,6 +485,56 @@ describe "Content Localization" do
expect(post_1_obj).to have_cooked_content(jap_post.cooked)
end
end
+
+ context "for tags" do
+ SWITCHER_SELECTOR = "button[data-identifier='language-switcher']"
+
+ let(:discovery) { PageObjects::Pages::Discovery.new }
+ let(:sidebar) { PageObjects::Components::NavigationMenu::Sidebar.new }
+ let(:switcher) { PageObjects::Components::DMenu.new(SWITCHER_SELECTOR) }
+
+ fab!(:tag) { Fabricate(:tag, name: "strategy", locale: "en") }
+ fab!(:tag_localization) { Fabricate(:tag_localization, tag:, locale: "ja", name: "戦略") }
+ fab!(:topic_tag) { Fabricate(:topic_tag, topic:, tag:) }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ SiteSetting.navigation_menu = "sidebar"
+ SiteSetting.default_navigation_menu_tags = tag.name
+ SiteSetting.set_locale_from_cookie = true
+ SiteSetting.content_localization_language_switcher = "all"
+ end
+
+ it "displays localized tag names in sidebar, topic list, tag dropdown, and topic view" do
+ sign_in(japanese_user)
+
+ visit("/")
+
+ expect(sidebar).to have_section_link("戦略")
+ expect(topic_list).to have_topic_tag(topic, "戦略")
+
+ discovery.tag_drop.expand
+ expect(discovery.tag_drop).to have_option_name("戦略")
+ discovery.tag_drop.collapse
+
+ topic_list.visit_topic_with_title("孫子兵法からの人生戦略")
+ expect(page).to have_css(".title-wrapper .discourse-tag", text: "戦略")
+
+ switcher.expand
+ switcher.option("[data-menu-option-id='en']").click
+
+ visit("/")
+ expect(sidebar).to have_section_link("strategy")
+ expect(topic_list).to have_topic_tag(topic, "strategy")
+
+ discovery.tag_drop.expand
+ expect(discovery.tag_drop).to have_option_name("strategy")
+ discovery.tag_drop.collapse
+
+ topic_list.visit_topic_with_title("Life strategies from The Art of War")
+ expect(page).to have_css(".title-wrapper .discourse-tag", text: "strategy")
+ end
+ end
end
context "with author localization" do