Skip to content

Commit 2c05a85

Browse files
authored
DEV: Use cookie instead of URL param to show original content (#273)
Currently when the "Show Original" button on the right side of the page is clicked, we append the param `?show=original` and trigger a refresh to show the original user-written post content. With this commit, we will be using cookies (`discourse-translator-show-original`) instead. This cookie is applied site-wide, not per-topic.
1 parent 2fa47c2 commit 2c05a85

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

assets/javascripts/discourse/components/show-original-content.gjs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { action } from "@ember/object";
44
import { service } from "@ember/service";
55
import DButton from "discourse/components/d-button";
66
import concatClass from "discourse/helpers/concat-class";
7+
import cookie, { removeCookie } from "discourse/lib/cookie";
8+
9+
const SHOW_ORIGINAL_COOKIE = "discourse-translator-show-original";
10+
const SHOW_ORIGINAL_COOKIE_EXPIRY = 30;
711

812
export default class ShowOriginalContent extends Component {
913
static shouldRender(args) {
@@ -15,38 +19,42 @@ export default class ShowOriginalContent extends Component {
1519

1620
@service router;
1721

18-
@tracked isTranslated = true;
22+
@tracked showingOriginal = false;
1923

2024
constructor() {
2125
super(...arguments);
22-
this.isTranslated = !new URLSearchParams(window.location.search).has(
23-
"show"
24-
);
26+
this.showingOriginal = cookie(SHOW_ORIGINAL_COOKIE);
2527
}
2628

2729
@action
2830
async showOriginal() {
29-
const params = new URLSearchParams(window.location.search);
30-
if (this.isTranslated) {
31-
params.append("show", "original");
31+
if (this.showingOriginal) {
32+
removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });
3233
} else {
33-
params.delete("show");
34+
cookie(SHOW_ORIGINAL_COOKIE, true, {
35+
path: "/",
36+
expires: SHOW_ORIGINAL_COOKIE_EXPIRY,
37+
});
3438
}
35-
window.location.search = params.toString();
39+
40+
this.router.refresh();
3641
}
3742

3843
get title() {
39-
return this.isTranslated
40-
? "translator.content_translated"
41-
: "translator.content_not_translated";
44+
return this.showingOriginal
45+
? "translator.content_not_translated"
46+
: "translator.content_translated";
4247
}
4348

4449
<template>
4550
<div class="discourse-translator_toggle-original">
4651
<DButton
4752
@icon="language"
4853
@title={{this.title}}
49-
class={{concatClass "btn btn-default" (if this.isTranslated "active")}}
54+
class={{concatClass
55+
"btn btn-default"
56+
(unless this.showingOriginal "active")
57+
}}
5058
@action={{this.showOriginal}}
5159
/>
5260
</div>

lib/discourse_translator/inline_translation.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ def self.effective_locale
1010
end
1111
end
1212

13+
SHOW_ORIGINAL_COOKIE = "discourse-translator-show-original"
14+
1315
def inject(plugin)
16+
plugin.register_anonymous_cache_key :showoriginal do
17+
@request.cookies[SHOW_ORIGINAL_COOKIE].present? ? "1" : "0"
18+
end
19+
1420
# since locales are eager loaded but translations may not,
1521
# always return early if topic and posts are in the user's effective_locale.
1622
# this prevents the need to load translations.
1723

1824
plugin.register_modifier(:basic_post_serializer_cooked) do |cooked, serializer|
1925
if !SiteSetting.experimental_inline_translation ||
2026
serializer.object.locale_matches?(InlineTranslation.effective_locale) ||
21-
serializer.scope&.request&.params&.[]("show") == "original"
27+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
2228
cooked
2329
else
2430
serializer.object.translation_for(InlineTranslation.effective_locale).presence
@@ -28,7 +34,7 @@ def inject(plugin)
2834
plugin.register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer|
2935
if !SiteSetting.experimental_inline_translation ||
3036
serializer.object.locale_matches?(InlineTranslation.effective_locale) ||
31-
serializer.scope&.request&.params&.[]("show") == "original"
37+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
3238
fancy_title
3339
else
3440
serializer
@@ -42,7 +48,7 @@ def inject(plugin)
4248
plugin.register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer|
4349
if !SiteSetting.experimental_inline_translation ||
4450
serializer.object.topic.locale_matches?(InlineTranslation.effective_locale) ||
45-
serializer.scope&.request&.params&.[]("show") == "original"
51+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
4652
fancy_title
4753
else
4854
serializer

spec/serializers/basic_topic_serializer_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
SiteSetting.automatic_translation_target_languages = "ja"
2626
end
2727

28-
def serialize_topic(guardian_user: user, params: {})
29-
env = { "action_dispatch.request.parameters" => params, "REQUEST_METHOD" => "GET" }
28+
def serialize_topic(guardian_user: user, cookie: "")
29+
env = create_request_env.merge("HTTP_COOKIE" => cookie)
3030
request = ActionDispatch::Request.new(env)
3131
guardian = Guardian.new(guardian_user, request)
3232
BasicTopicSerializer.new(topic, scope: guardian)
@@ -41,7 +41,11 @@ def serialize_topic(guardian_user: user, params: {})
4141

4242
it "does not replace fancy_title with translation when show_original param is present" do
4343
topic.set_translation("ja", jap_title)
44-
expect(serialize_topic(params: { "show" => "original" }).fancy_title).to eq(topic.fancy_title)
44+
expect(
45+
serialize_topic(
46+
cookie: DiscourseTranslator::InlineTranslation::SHOW_ORIGINAL_COOKIE,
47+
).fancy_title,
48+
).to eq(topic.fancy_title)
4549
end
4650

4751
it "does not replace fancy_title with translation when no translation exists" do

spec/serializers/post_serializer_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
end
115115

116116
describe "#cooked" do
117-
def serialize_post(guardian_user: user, params: {})
118-
env = { "action_dispatch.request.parameters" => params, "REQUEST_METHOD" => "GET" }
117+
def serialize_post(guardian_user: user, cookie: "")
118+
env = create_request_env.merge("HTTP_COOKIE" => cookie)
119119
request = ActionDispatch::Request.new(env)
120120
guardian = Guardian.new(guardian_user, request)
121121
PostSerializer.new(post, scope: guardian)
@@ -131,14 +131,16 @@ def serialize_post(guardian_user: user, params: {})
131131
expect(serialize_post.cooked).to eq(post.cooked)
132132
end
133133

134-
it "does not return translated_cooked when show=original param is present" do
134+
it "does not return translated_cooked when show original translation cookie is present" do
135135
I18n.locale = "ja"
136136
SiteSetting.automatic_translation_backfill_rate = 1
137137
SiteSetting.automatic_translation_target_languages = "ja"
138138
post.set_translation("ja", "こんにちは")
139139

140-
expect(serialize_post(params: { "show" => "original" }).cooked).to eq(post.cooked)
141-
expect(serialize_post(params: { "show" => "derp" }).cooked).to eq("こんにちは")
140+
expect(
141+
serialize_post(cookie: DiscourseTranslator::InlineTranslation::SHOW_ORIGINAL_COOKIE).cooked,
142+
).to eq(post.cooked)
143+
expect(serialize_post(cookie: "derp").cooked).to eq("こんにちは")
142144
end
143145

144146
it "does not return translated_cooked when post is already in correct locale" do

spec/system/full_page_translation_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
end
1515

1616
let(:topic_page) { PageObjects::Pages::Topic.new }
17+
let(:topic_list) { PageObjects::Components::TopicList.new }
1718

1819
before do
1920
# topic translation setup
@@ -53,14 +54,21 @@
5354
visit("/")
5455
visit("/t/#{topic.id}")
5556
expect(topic_page.has_topic_title?("孫子兵法からの人生戦略")).to eq(true)
57+
end
58+
59+
it "shows original content when 'Show Original' is selected" do
60+
sign_in(japanese_user)
5661

62+
visit("/")
63+
topic_list.visit_topic_with_title("孫子兵法からの人生戦略")
64+
65+
expect(topic_page.has_topic_title?("孫子兵法からの人生戦略")).to eq(true)
5766
page.find(".discourse-translator_toggle-original button").click
58-
expect(page).to have_current_path(/.*show=original.*/)
5967

6068
expect(topic_page.has_topic_title?("Life strategies from The Art of War")).to eq(true)
61-
expect(find(topic_page.post_by_number_selector(1))).to have_content(
62-
"The masterpiece isn’t just about military strategy",
63-
)
69+
70+
visit("/")
71+
topic_list.visit_topic_with_title("Life strategies from The Art of War")
6472
end
6573
end
6674
end

0 commit comments

Comments
 (0)