Skip to content

Commit c61a6f8

Browse files
authored
FEATURE: Localize topic titles in notifications and bookmarks (#34059)
Localizes topic titles in these areas - user notification - bookmarks This commit also updates the user notification bookmark list to use fancy title instead of title, similar to the other user notification tabs.
1 parent acf3d73 commit c61a6f8

File tree

12 files changed

+138
-40
lines changed

12 files changed

+138
-40
lines changed

app/assets/javascripts/discourse/app/lib/user-menu/bookmark-item.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default class UserMenuBookmarkItem extends UserMenuBaseItem {
3232
}
3333

3434
get description() {
35-
return this.bookmark.title;
35+
return this.bookmark.fancy_title;
3636
}
3737

3838
get topicId() {

app/assets/javascripts/discourse/tests/acceptance/user-menu-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ acceptance("User menu", function (needs) {
357357
withPluginApi((api) => {
358358
api.registerModelTransformer("bookmark", (bookmarks) => {
359359
bookmarks.forEach((bookmark) => {
360-
if (bookmark.title) {
361-
bookmark.title = `pluginBookmarkTransformer ${bookmark.title}`;
360+
if (bookmark.fancy_title) {
361+
bookmark.fancy_title = `pluginBookmarkTransformer ${bookmark.fancy_title}`;
362362
}
363363
});
364364
});

app/assets/javascripts/discourse/tests/integration/components/user-menu/menu-item-test.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ module(
536536

537537
test("item description is the bookmark title", async function (assert) {
538538
const item = getBookmark(
539-
{ title: "Custom bookmark title" },
539+
{ fancy_title: "Custom bookmark title" },
540540
this.siteSettings,
541541
this.site
542542
);

app/models/topic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@ def has_localization?(locale = I18n.locale)
21412141
end
21422142

21432143
def in_user_locale?
2144-
locale == I18n.locale.to_s
2144+
LocaleNormalizer.is_same?(locale, I18n.locale)
21452145
end
21462146

21472147
def get_localization(locale = I18n.locale)

app/serializers/basic_topic_serializer.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22

33
# The most basic attributes of a topic that we need to create a link for it.
44
class BasicTopicSerializer < ApplicationSerializer
5-
attributes :id, :title, :fancy_title, :slug, :posts_count
6-
7-
def fancy_title
8-
f = object.fancy_title
5+
include LocalizedFancyTopicTitleMixin
96

10-
if (ContentLocalization.show_translated_topic?(object, scope))
11-
object.get_localization&.fancy_title.presence || f
12-
else
13-
f
14-
end
15-
end
7+
attributes :id, :title, :fancy_title, :slug, :posts_count
168
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
module LocalizedFancyTopicTitleMixin
4+
def self.included(klass)
5+
klass.attributes :fancy_title
6+
end
7+
8+
def fancy_title
9+
f = _topic.fancy_title
10+
11+
if (ContentLocalization.show_translated_topic?(_topic, scope))
12+
_topic.get_localization&.fancy_title.presence || f
13+
else
14+
f
15+
end
16+
end
17+
18+
def include_fancy_title?
19+
_topic.present? && _topic&.fancy_title.present?
20+
end
21+
22+
private
23+
24+
def _topic
25+
return object if object.class == Topic
26+
return topic if defined?(topic) && topic.class == Topic
27+
object.topic if defined?(object.topic) && object.topic.class == Topic
28+
end
29+
end

app/serializers/notification_serializer.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class NotificationSerializer < ApplicationSerializer
4+
include LocalizedFancyTopicTitleMixin
5+
46
attributes :id,
57
:user_id,
68
:external_id,
@@ -25,14 +27,6 @@ def is_warning
2527
object.topic.present? && object.topic.subtype == TopicSubtype.moderator_warning
2628
end
2729

28-
def include_fancy_title?
29-
object.topic&.fancy_title
30-
end
31-
32-
def fancy_title
33-
object.topic.fancy_title
34-
end
35-
3630
def include_is_warning?
3731
is_warning
3832
end

app/serializers/topic_view_serializer.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class TopicViewSerializer < ApplicationSerializer
55
include SuggestedTopicsMixin
66
include TopicTagsMixin
77
include ApplicationHelper
8+
include LocalizedFancyTopicTitleMixin
89

910
def self.attributes_from_topic(*list)
1011
[list].flatten.each do |attribute|
@@ -18,7 +19,6 @@ def self.attributes_from_topic(*list)
1819
attributes_from_topic(
1920
:id,
2021
:title,
21-
:fancy_title,
2222
:posts_count,
2323
:created_at,
2424
:views,
@@ -319,16 +319,6 @@ def include_visibility_reason_id?
319319
object.topic.visibility_reason_id.present?
320320
end
321321

322-
def fancy_title
323-
f = object.topic.fancy_title
324-
325-
if ContentLocalization.show_translated_topic?(object.topic, scope)
326-
object.topic.get_localization&.fancy_title.presence || f
327-
else
328-
f
329-
end
330-
end
331-
332322
def has_localized_content
333323
topic_has_localization = !object.topic.in_user_locale? && object.topic.has_localization?
334324
return true if topic_has_localization

app/serializers/user_post_topic_bookmark_base_serializer.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class UserPostTopicBookmarkBaseSerializer < UserBookmarkBaseSerializer
66
include TopicTagsMixin
77
include PostItemExcerpt
8+
include LocalizedFancyTopicTitleMixin
89

910
attributes :topic_id,
1011
:linked_post_number,
@@ -26,10 +27,6 @@ def title
2627
topic.title
2728
end
2829

29-
def fancy_title
30-
topic.fancy_title
31-
end
32-
3330
def category_id
3431
topic.category_id
3532
end

spec/models/topic_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3673,6 +3673,9 @@ def set_state!(group, user, state)
36733673

36743674
expect(topic.in_user_locale?).to eq(true)
36753675

3676+
topic.update!(locale: "ja_JP")
3677+
expect(topic.in_user_locale?).to eq(true)
3678+
36763679
topic.update!(locale: "es")
36773680
expect(topic.in_user_locale?).to eq(false)
36783681
end

0 commit comments

Comments
 (0)