Skip to content

Commit a90d737

Browse files
authored
FIX: Do not translate new posts if show=original (#271)
When a new post is created, and a user is in "show original" translation mode when inline translation is enabled, the new post comes in translated. This commit fixes that by registering a custom post message callback, and checking if the user is in "show original".
1 parent 528b422 commit a90d737

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

app/jobs/regular/translate_translatable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def execute(args)
1919

2020
topic_id, post_id =
2121
translatable.is_a?(Post) ? [translatable.topic_id, translatable.id] : [translatable.id, 1]
22-
MessageBus.publish("/topic/#{topic_id}", type: :revised, id: post_id)
22+
MessageBus.publish("/topic/#{topic_id}", type: :translated_post, id: post_id)
2323
end
2424
end
2525
end

assets/javascripts/discourse/initializers/extend-for-translate-button.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ function initializeTranslation(api) {
2929
(currentUser || siteSettings.experimental_anon_language_switcher)
3030
) {
3131
api.renderInOutlet("topic-navigation", ShowOriginalContent);
32+
33+
api.registerCustomPostMessageCallback(
34+
"translated_post",
35+
(topicController, data) => {
36+
if (
37+
new URLSearchParams(window.location.search).get("show") === "original"
38+
) {
39+
return;
40+
}
41+
const postStream = topicController.get("model.postStream");
42+
postStream.triggerChangedPost(data.id, data.updated_at).then(() => {
43+
topicController.appEvents.trigger("post-stream:refresh", {
44+
id: data.id,
45+
});
46+
});
47+
}
48+
);
3249
}
3350

3451
customizePostMenu(api);

spec/jobs/translate_translatable_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
end
3232

3333
it "translates posts to configured target languages" do
34-
MessageBus.expects(:publish).with("/topic/#{post.topic.id}", type: :revised, id: post.id).once
34+
MessageBus
35+
.expects(:publish)
36+
.with("/topic/#{post.topic.id}", type: :translated_post, id: post.id)
37+
.once
3538

3639
job.execute(type: "Post", translatable_id: post.id)
3740

@@ -40,7 +43,7 @@
4043
end
4144

4245
it "translates topics to configured target languages" do
43-
MessageBus.expects(:publish).with("/topic/#{topic.id}", type: :revised, id: 1).once
46+
MessageBus.expects(:publish).with("/topic/#{topic.id}", type: :translated_post, id: 1).once
4447

4548
job.execute(type: "Topic", translatable_id: topic.id)
4649

0 commit comments

Comments
 (0)