Skip to content

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

FEATURE: Localize topic titles in notifications and bookmarks (#34059)

Natalie Tayc61a6f83e77b
diff --git a/app/assets/javascripts/discourse/app/lib/user-menu/bookmark-item.js b/app/assets/javascripts/discourse/app/lib/user-menu/bookmark-item.js
index 5a69a4f7b17..c66f8db3a06 100644
--- a/app/assets/javascripts/discourse/app/lib/user-menu/bookmark-item.js
+++ b/app/assets/javascripts/discourse/app/lib/user-menu/bookmark-item.js
@@ -32,7 +32,7 @@ export default class UserMenuBookmarkItem extends UserMenuBaseItem {
}
get description() {
- return this.bookmark.title;
+ return this.bookmark.fancy_title;
}
get topicId() {
diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js
index 76662adf1d2..95f40843d0c 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js
@@ -357,8 +357,8 @@ acceptance("User menu", function (needs) {
withPluginApi((api) => {
api.registerModelTransformer("bookmark", (bookmarks) => {
bookmarks.forEach((bookmark) => {
- if (bookmark.title) {
- bookmark.title = `pluginBookmarkTransformer ${bookmark.title}`;
+ if (bookmark.fancy_title) {
+ bookmark.fancy_title = `pluginBookmarkTransformer ${bookmark.fancy_title}`;
}
});
});
diff --git a/app/assets/javascripts/discourse/tests/integration/components/user-menu/menu-item-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/user-menu/menu-item-test.gjs
index 68007183d1a..7f04161b9b7 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/user-menu/menu-item-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/user-menu/menu-item-test.gjs
@@ -536,7 +536,7 @@ module(
test("item description is the bookmark title", async function (assert) {
const item = getBookmark(
- { title: "Custom bookmark title" },
+ { fancy_title: "Custom bookmark title" },
this.siteSettings,
this.site
);
diff --git a/app/models/topic.rb b/app/models/topic.rb
index b541ed8f778..b5220ef92ea 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -2141,7 +2141,7 @@ class Topic < ActiveRecord::Base
end
def in_user_locale?
- locale == I18n.locale.to_s
+ LocaleNormalizer.is_same?(locale, I18n.locale)
end
def get_localization(locale = I18n.locale)
diff --git a/app/serializers/basic_topic_serializer.rb b/app/serializers/basic_topic_serializer.rb
index 3f7b2fb0102..1717232fe15 100644
--- a/app/serializers/basic_topic_serializer.rb
+++ b/app/serializers/basic_topic_serializer.rb
@@ -2,15 +2,7 @@
# The most basic attributes of a topic that we need to create a link for it.
class BasicTopicSerializer < ApplicationSerializer
- attributes :id, :title, :fancy_title, :slug, :posts_count
-
- def fancy_title
- f = object.fancy_title
+ include LocalizedFancyTopicTitleMixin
- if (ContentLocalization.show_translated_topic?(object, scope))
- object.get_localization&.fancy_title.presence || f
- else
- f
- end
- end
+ attributes :id, :title, :fancy_title, :slug, :posts_count
end
diff --git a/app/serializers/concerns/localized_fancy_topic_title_mixin.rb b/app/serializers/concerns/localized_fancy_topic_title_mixin.rb
new file mode 100644
index 00000000000..0301073a33b
--- /dev/null
+++ b/app/serializers/concerns/localized_fancy_topic_title_mixin.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module LocalizedFancyTopicTitleMixin
+ def self.included(klass)
+ klass.attributes :fancy_title
+ end
+
+ def fancy_title
+ f = _topic.fancy_title
+
+ if (ContentLocalization.show_translated_topic?(_topic, scope))
+ _topic.get_localization&.fancy_title.presence || f
+ else
+ f
+ end
+ end
+
+ def include_fancy_title?
+ _topic.present? && _topic&.fancy_title.present?
+ end
+
+ private
+
+ def _topic
+ return object if object.class == Topic
+ return topic if defined?(topic) && topic.class == Topic
+ object.topic if defined?(object.topic) && object.topic.class == Topic
+ end
+end
diff --git a/app/serializers/notification_serializer.rb b/app/serializers/notification_serializer.rb
index 546cbe4b63b..fa71921e682 100644
--- a/app/serializers/notification_serializer.rb
+++ b/app/serializers/notification_serializer.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
class NotificationSerializer < ApplicationSerializer
+ include LocalizedFancyTopicTitleMixin
+
attributes :id,
:user_id,
:external_id,
@@ -25,14 +27,6 @@ class NotificationSerializer < ApplicationSerializer
object.topic.present? && object.topic.subtype == TopicSubtype.moderator_warning
end
- def include_fancy_title?
- object.topic&.fancy_title
- end
-
- def fancy_title
- object.topic.fancy_title
- end
-
def include_is_warning?
is_warning
end
diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb
index f6104dbf007..39b56e54ef2 100644
--- a/app/serializers/topic_view_serializer.rb
+++ b/app/serializers/topic_view_serializer.rb
@@ -5,6 +5,7 @@ class TopicViewSerializer < ApplicationSerializer
include SuggestedTopicsMixin
include TopicTagsMixin
include ApplicationHelper
+ include LocalizedFancyTopicTitleMixin
def self.attributes_from_topic(*list)
[list].flatten.each do |attribute|
@@ -18,7 +19,6 @@ class TopicViewSerializer < ApplicationSerializer
attributes_from_topic(
:id,
:title,
- :fancy_title,
:posts_count,
:created_at,
:views,
@@ -319,16 +319,6 @@ class TopicViewSerializer < ApplicationSerializer
object.topic.visibility_reason_id.present?
end
- def fancy_title
- f = object.topic.fancy_title
-
- if ContentLocalization.show_translated_topic?(object.topic, scope)
- object.topic.get_localization&.fancy_title.presence || f
- else
- f
- end
- end
-
def has_localized_content
topic_has_localization = !object.topic.in_user_locale? && object.topic.has_localization?
return true if topic_has_localization
diff --git a/app/serializers/user_post_topic_bookmark_base_serializer.rb b/app/serializers/user_post_topic_bookmark_base_serializer.rb
index 02798d061ad..524aeafb23d 100644
--- a/app/serializers/user_post_topic_bookmark_base_serializer.rb
+++ b/app/serializers/user_post_topic_bookmark_base_serializer.rb
@@ -5,6 +5,7 @@ require_relative "post_item_excerpt"
class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
include TopicTagsMixin
include PostItemExcerpt
+ include LocalizedFancyTopicTitleMixin
attributes :topic_id,
:linked_post_number,
@@ -26,10 +27,6 @@ class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
topic.title
end
- def fancy_title
- topic.fancy_title
- end
-
def category_id
topic.category_id
end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 49ea9e3479f..9b797fa5530 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -3673,6 +3673,9 @@ describe Topic do
expect(topic.in_user_locale?).to eq(true)
+ topic.update!(locale: "ja_JP")
+ expect(topic.in_user_locale?).to eq(true)
+
topic.update!(locale: "es")
expect(topic.in_user_locale?).to eq(false)
end
diff --git a/spec/requests/notifications_controller_spec.rb b/spec/requests/notifications_controller_spec.rb
index 5181f317b87..92cf27ce250 100644
--- a/spec/requests/notifications_controller_spec.rb
+++ b/spec/requests/notifications_controller_spec.rb
@@ -441,6 +441,58 @@ RSpec.describe NotificationsController do
end
end
end
+
+ context "when a notification topic has localizations" do
+ fab!(:english_topic) { Fabricate(:topic, locale: "en") }
+ fab!(:topic_localization_es) do
+ Fabricate(
+ :topic_localization,
+ topic: english_topic,
+ locale: "es",
+ fancy_title: "Hola Mundo",
+ )
+ end
+ fab!(:topic_localization_ja) do
+ Fabricate(
+ :topic_localization,
+ topic: english_topic,
+ locale: "ja",
+ fancy_title: "こんにちは世界",
+ )
+ end
+ fab!(:notification) do
+ Fabricate(
+ :notification,
+ topic: english_topic,
+ user:,
+ notification_type: Notification.types[:liked],
+ )
+ end
+
+ it "displays the localized fancy title in the user's locale when content_localization_enabled enabled" do
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.allow_user_locale = true
+ user.update!(locale: "ja")
+
+ get "/notifications.json"
+
+ expect(response.status).to eq(200)
+ notifications = response.parsed_body["notifications"]
+ expect(notifications.first["fancy_title"]).to eq(topic_localization_ja.fancy_title)
+ end
+
+ it "does not display the localized fancy title in the user's locale when content_localization_enabled disabled" do
+ SiteSetting.content_localization_enabled = false
+ SiteSetting.allow_user_locale = true
+ user.update!(locale: "ja")
+
+ get "/notifications.json"
+
+ expect(response.status).to eq(200)
+ notifications = response.parsed_body["notifications"]
+ expect(notifications.first["fancy_title"]).to eq(english_topic.fancy_title)
+ end
+ end
end
it "should succeed" do
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index 11eb9cc9f98..b85573c37b7 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -7212,8 +7212,10 @@ RSpec.describe UsersController do
after { DiscoursePluginRegistry.reset! }
- let(:bookmark1) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:post)) }
- let(:bookmark2) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:topic)) }
+ fab!(:topic)
+ fab!(:post)
+ let(:bookmark1) { Fabricate(:bookmark, user: user1, bookmarkable: post) }
+ let(:bookmark2) { Fabricate(:bookmark, user: user1, bookmarkable: topic) }
let(:bookmark3) { Fabricate(:bookmark, user: user1, bookmarkable: Fabricate(:user)) }
let(:bookmark4) { Fabricate(:bookmark) }
@@ -7314,6 +7316,45 @@ RSpec.describe UsersController do
"/u/someusername/bookmarks.json",
described_class::BOOKMARKS_LIMIT
end
+
+ describe "when a notification topic has localizations" do
+ fab!(:jap_user) { Fabricate(:user, locale: "ja") }
+ fab!(:topic_localization_es) do
+ Fabricate(:topic_localization, topic:, locale: "es", fancy_title: "Hola Mundo")
+ end
+ fab!(:topic_localization_ja) do
+ Fabricate(:topic_localization, topic:, locale: "ja", fancy_title: "こんにちは世界")
+ end
+
+ before do
+ topic.update(locale: "en")
+ user1.update(locale: "ja")
+ end
+
+ it "displays the localized fancy title in the user's locale when content_localization_enabled enabled" do
+ SiteSetting.content_localization_enabled = true
+ SiteSetting.allow_user_locale = true
+ sign_in(user1)
+
+ get "/u/#{user1.username}/bookmarks.json"
+ expect(response.status).to eq(200)
+ response_bookmarks = response.parsed_body["user_bookmark_list"]["bookmarks"]
+ expect(response_bookmarks.map { |b| b["fancy_title"] }).to include(
+ topic_localization_ja.fancy_title,
+ )
+ end
+
+ it "does not display the localized fancy title in the user's locale when content_localization_enabled disabled" do
+ SiteSetting.content_localization_enabled = false
+ SiteSetting.allow_user_locale = true
+ sign_in(user1)
+
+ get "/u/#{user1.username}/bookmarks.json"
+ expect(response.status).to eq(200)
+ response_bookmarks = response.parsed_body["user_bookmark_list"]["bookmarks"]
+ expect(response_bookmarks.map { |b| b["fancy_title"] }).to include(topic.fancy_title)
+ end
+ end
end
describe "#bookmarks excerpts" do