Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,25 @@ module ::DiscourseTranslator
register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer|
if !SiteSetting.experimental_topic_translation ||
serializer.scope.request.params["show"] == "original" ||
serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-")
serializer.object.locale_matches?(I18n.locale)
fancy_title
else
serializer.object.translation_for(I18n.locale).presence
serializer.object.translation_for(I18n.locale).presence&.then { |t| Topic.fancy_title(t) }
end
end

register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer|
if !SiteSetting.experimental_topic_translation ||
serializer.scope.request.params["show"] == "original" ||
serializer.object.topic.detected_locale == I18n.locale.to_s.gsub("_", "-")
serializer.object.topic.locale_matches?(I18n.locale)
fancy_title
else
serializer.object.topic.translation_for(I18n.locale).presence
serializer
.object
.topic
.translation_for(I18n.locale)
.presence
&.then { |t| Topic.fancy_title(t) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh TIL about #then, pretty neat! What happens if we get blank translation? Should we fallback to the original? Or is this handled on the client side?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modifier in core handles the case when this value is nil. (discourse/discourse@5f00ae2)

end
end
end
6 changes: 3 additions & 3 deletions spec/serializers/basic_topic_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

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

before do
topic.title = original_title
Expand Down Expand Up @@ -55,7 +55,7 @@ def serialize_topic(guardian_user: user, params: {})

it "returns translated title in fancy_title when translation exists for current locale" do
topic.set_translation("ja", jap_title)
expect(serialize_topic.fancy_title).to eq(jap_title)
expect(serialize_topic.fancy_title).to eq("&lt;h1&gt;フス・ロ・ダ・ア&lt;/h1&gt;")
end
end
end
6 changes: 3 additions & 3 deletions spec/serializers/topic_view_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
fab!(:topic)

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

before do
topic.title = original_title
Expand Down Expand Up @@ -78,7 +78,7 @@ def serialize_topic(guardian_user: user, params: {})

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