Skip to content

Commit e0105f8

Browse files
authored
FIX: Fancify titles when using experimental topic translation (#224)
In the existing method of translation, we show the translated titles below the post cooked. When using the same already-translated titles for the experimental feature, it would result in the H1 rendering into large text in the topic title. We've been sending the `topic.title` in for translation, instead of the escaped `topic.fancy_title`, which works well for the existing method. I had considered changing our implementation to [send the fancy_titles (escaped) in for translation instead](#222), but that would result in our existing methods titles being double-escaped. To keep compatibility between existing and experimental, I will only escape it for the experimental feature. This commit does that.
1 parent 5f24835 commit e0105f8

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

plugin.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,25 @@ module ::DiscourseTranslator
7373
register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer|
7474
if !SiteSetting.experimental_topic_translation ||
7575
serializer.scope.request.params["show"] == "original" ||
76-
serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-")
76+
serializer.object.locale_matches?(I18n.locale)
7777
fancy_title
7878
else
79-
serializer.object.translation_for(I18n.locale).presence
79+
serializer.object.translation_for(I18n.locale).presence&.then { |t| Topic.fancy_title(t) }
8080
end
8181
end
8282

8383
register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer|
8484
if !SiteSetting.experimental_topic_translation ||
8585
serializer.scope.request.params["show"] == "original" ||
86-
serializer.object.topic.detected_locale == I18n.locale.to_s.gsub("_", "-")
86+
serializer.object.topic.locale_matches?(I18n.locale)
8787
fancy_title
8888
else
89-
serializer.object.topic.translation_for(I18n.locale).presence
89+
serializer
90+
.object
91+
.topic
92+
.translation_for(I18n.locale)
93+
.presence
94+
&.then { |t| Topic.fancy_title(t) }
9095
end
9196
end
9297
end

spec/serializers/basic_topic_serializer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
describe "#fancy_title" do
1515
let!(:guardian) { Guardian.new(user) }
16-
let!(:original_title) { "FUS ROH DAAHHH" }
17-
let!(:jap_title) { "フス・ロ・ダ・ア" }
16+
let!(:original_title) { "<h1>FUS ROH DAAHHH</h1>" }
17+
let!(:jap_title) { "<h1>フス・ロ・ダ・ア</h1>" }
1818

1919
before do
2020
topic.title = original_title
@@ -55,7 +55,7 @@ def serialize_topic(guardian_user: user, params: {})
5555

5656
it "returns translated title in fancy_title when translation exists for current locale" do
5757
topic.set_translation("ja", jap_title)
58-
expect(serialize_topic.fancy_title).to eq(jap_title)
58+
expect(serialize_topic.fancy_title).to eq("&lt;h1&gt;フス・ロ・ダ・ア&lt;/h1&gt;")
5959
end
6060
end
6161
end

spec/serializers/topic_view_serializer_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
fab!(:topic)
3737

3838
let!(:guardian) { Guardian.new(user) }
39-
let!(:original_title) { "FUS ROH DAAHHH" }
40-
let!(:jap_title) { "フス・ロ・ダ・ア" }
39+
let!(:original_title) { "<h1>FUS ROH DAAHHH</h1>" }
40+
let!(:jap_title) { "<h1>フス・ロ・ダ・ア</h1>" }
4141

4242
before do
4343
topic.title = original_title
@@ -78,7 +78,7 @@ def serialize_topic(guardian_user: user, params: {})
7878

7979
it "returns translated title in fancy_title when translation exists for current locale" do
8080
topic.set_translation("ja", jap_title)
81-
expect(serialize_topic.fancy_title).to eq(jap_title)
81+
expect(serialize_topic.fancy_title).to eq("&lt;h1&gt;フス・ロ・ダ・ア&lt;/h1&gt;")
8282
end
8383
end
8484
end

0 commit comments

Comments
 (0)