Skip to content

Commit aafb500

Browse files
committed
DEV: Use cookie instead of URL param to show original content
1 parent a90d737 commit aafb500

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import Component from "@glimmer/component";
22
import { tracked } from "@glimmer/tracking";
33
import { action } from "@ember/object";
44
import { service } from "@ember/service";
5+
import { not } from "truth-helpers";
56
import DButton from "discourse/components/d-button";
67
import concatClass from "discourse/helpers/concat-class";
8+
import cookie, { removeCookie } from "discourse/lib/cookie";
9+
10+
const SHOW_ORIGINAL_COOKIE = "discourse-translator-show-original";
11+
const SHOW_ORIGINAL_COOKIE_EXPIRY = 30;
712

813
export default class ShowOriginalContent extends Component {
914
static shouldRender(args) {
@@ -15,38 +20,42 @@ export default class ShowOriginalContent extends Component {
1520

1621
@service router;
1722

18-
@tracked isTranslated = true;
23+
@tracked showingOriginal = false;
1924

2025
constructor() {
2126
super(...arguments);
22-
this.isTranslated = !new URLSearchParams(window.location.search).has(
23-
"show"
24-
);
27+
this.showingOriginal = cookie(SHOW_ORIGINAL_COOKIE);
2528
}
2629

2730
@action
2831
async showOriginal() {
29-
const params = new URLSearchParams(window.location.search);
30-
if (this.isTranslated) {
31-
params.append("show", "original");
32+
if (this.showingOriginal) {
33+
removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });
3234
} else {
33-
params.delete("show");
35+
cookie(SHOW_ORIGINAL_COOKIE, true, {
36+
path: "/",
37+
expires: SHOW_ORIGINAL_COOKIE_EXPIRY,
38+
});
3439
}
35-
window.location.search = params.toString();
40+
41+
window.location.reload();
3642
}
3743

3844
get title() {
39-
return this.isTranslated
40-
? "translator.content_translated"
41-
: "translator.content_not_translated";
45+
return this.showingOriginal
46+
? "translator.content_not_translated"
47+
: "translator.content_translated";
4248
}
4349

4450
<template>
4551
<div class="discourse-translator_toggle-original">
4652
<DButton
4753
@icon="language"
4854
@title={{this.title}}
49-
class={{concatClass "btn btn-default" (if this.isTranslated "active")}}
55+
class={{concatClass
56+
"btn btn-default"
57+
(if (not this.showingOriginal) "active")
58+
}}
5059
@action={{this.showOriginal}}
5160
/>
5261
</div>

lib/discourse_translator/inline_translation.rb

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

13+
SHOW_ORIGINAL_COOKIE = "discourse-translator-show-original"
14+
1315
def inject(plugin)
1416
# since locales are eager loaded but translations may not,
1517
# always return early if topic and posts are in the user's effective_locale.
@@ -18,7 +20,7 @@ def inject(plugin)
1820
plugin.register_modifier(:basic_post_serializer_cooked) do |cooked, serializer|
1921
if !SiteSetting.experimental_inline_translation ||
2022
serializer.object.locale_matches?(InlineTranslation.effective_locale) ||
21-
serializer.scope&.request&.params&.[]("show") == "original"
23+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
2224
cooked
2325
else
2426
serializer.object.translation_for(InlineTranslation.effective_locale).presence
@@ -28,7 +30,7 @@ def inject(plugin)
2830
plugin.register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer|
2931
if !SiteSetting.experimental_inline_translation ||
3032
serializer.object.locale_matches?(InlineTranslation.effective_locale) ||
31-
serializer.scope&.request&.params&.[]("show") == "original"
33+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
3234
fancy_title
3335
else
3436
serializer
@@ -42,7 +44,7 @@ def inject(plugin)
4244
plugin.register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer|
4345
if !SiteSetting.experimental_inline_translation ||
4446
serializer.object.topic.locale_matches?(InlineTranslation.effective_locale) ||
45-
serializer.scope&.request&.params&.[]("show") == "original"
47+
serializer.scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
4648
fancy_title
4749
else
4850
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)