Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: A dedicated 'show original' user preferences saved to user options (#39128)

Natalie Taycd6427203c4f
diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb
index 7399094cb99..7819136987f 100644
--- a/app/helpers/topics_helper.rb
+++ b/app/helpers/topics_helper.rb
@@ -23,6 +23,7 @@ module TopicsHelper
def localize_topic_view_content(topic_view)
return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)
+ return if current_user&.user_option&.show_original_content
# locale param is appropriately set in the application controller
# depending on site settings and presence of user
diff --git a/app/models/user_option.rb b/app/models/user_option.rb
index 909badf0687..143f6f59a0b 100644
--- a/app/models/user_option.rb
+++ b/app/models/user_option.rb
@@ -296,6 +296,7 @@ end
# notify_on_linked_posts :boolean default(TRUE), not null
# oldest_search_log_date :datetime
# seen_popups :integer is an Array
+# show_original_content :boolean default(FALSE), not null
# sidebar_link_to_filtered_list :boolean default(FALSE), not null
# sidebar_show_count_of_new_items :boolean default(FALSE), not null
# skip_new_user_tips :boolean default(FALSE), not null
diff --git a/app/serializers/current_user_option_serializer.rb b/app/serializers/current_user_option_serializer.rb
index 741a2085c63..ae1dfe1a935 100644
--- a/app/serializers/current_user_option_serializer.rb
+++ b/app/serializers/current_user_option_serializer.rb
@@ -26,7 +26,8 @@ class CurrentUserOptionSerializer < ApplicationSerializer
:sidebar_link_to_filtered_list,
:sidebar_show_count_of_new_items,
:composition_mode,
- :interface_color_mode
+ :interface_color_mode,
+ :show_original_content
def likes_notifications_disabled
object.likes_notifications_disabled?
diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rb
index 62a5d7a3fa0..18e5fefbcbd 100644
--- a/app/serializers/user_option_serializer.rb
+++ b/app/serializers/user_option_serializer.rb
@@ -47,7 +47,8 @@ class UserOptionSerializer < ApplicationSerializer
:watched_precedence_over_muted,
:topics_unread_when_closed,
:composition_mode,
- :interface_color_mode
+ :interface_color_mode,
+ :show_original_content
def auto_track_topics_after_msecs
object.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecs
diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb
index f9187c7c5c0..7ec8e4bbd66 100644
--- a/app/services/user_updater.rb
+++ b/app/services/user_updater.rb
@@ -59,6 +59,7 @@ class UserUpdater
watched_precedence_over_muted
topics_unread_when_closed
composition_mode
+ show_original_content
]
NOTIFICATION_SCHEDULE_ATTRS = -> do
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 62a04d1c47a..b4bdfcc1715 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -1721,6 +1721,7 @@ en:
enable_smart_lists: "Enable smart lists when writing in the composer"
enable_defer: "Enable mark topics as unread"
enable_markdown_monospace_font: "Use monospace font in composer's Markdown mode"
+ show_original_content: "Always show original content instead of translated content"
experimental_sidebar:
enable: "Enable sidebar"
options: "Options"
diff --git a/db/migrate/20260407093145_add_show_original_content_to_user_options.rb b/db/migrate/20260407093145_add_show_original_content_to_user_options.rb
new file mode 100644
index 00000000000..6630e1d0d69
--- /dev/null
+++ b/db/migrate/20260407093145_add_show_original_content_to_user_options.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddShowOriginalContentToUserOptions < ActiveRecord::Migration[7.2]
+ def change
+ add_column :user_options, :show_original_content, :boolean, default: false, null: false
+ end
+end
diff --git a/frontend/discourse/app/components/language-switcher.gjs b/frontend/discourse/app/components/language-switcher.gjs
index 3b63a827c2f..83dc62f019f 100644
--- a/frontend/discourse/app/components/language-switcher.gjs
+++ b/frontend/discourse/app/components/language-switcher.gjs
@@ -6,6 +6,7 @@ import DButton from "discourse/components/d-button";
import DropdownMenu from "discourse/components/dropdown-menu";
import DMenu from "discourse/float-kit/components/d-menu";
import icon from "discourse/helpers/d-icon";
+import { ajax } from "discourse/lib/ajax";
import cookie, { removeCookie } from "discourse/lib/cookie";
import I18n, { i18n } from "discourse-i18n";
@@ -20,7 +21,11 @@ export default class LanguageSwitcher extends Component {
async changeLocale(locale) {
if (this.currentUser) {
this.currentUser.set("locale", locale);
- await this.currentUser.save(["locale"]);
+ this.currentUser.set("user_option.show_original_content", false);
+ await ajax(`/u/${this.currentUser.username}.json`, {
+ type: "PUT",
+ data: { locale, show_original_content: false },
+ });
} else {
cookie("locale", locale, { path: "/" });
}
diff --git a/frontend/discourse/app/components/topic-localized-content-toggle.gjs b/frontend/discourse/app/components/topic-localized-content-toggle.gjs
index cf0e702d7d8..f3175b4205d 100644
--- a/frontend/discourse/app/components/topic-localized-content-toggle.gjs
+++ b/frontend/discourse/app/components/topic-localized-content-toggle.gjs
@@ -4,6 +4,7 @@ import { action } from "@ember/object";
import { service } from "@ember/service";
import DButton from "discourse/components/d-button";
import concatClass from "discourse/helpers/concat-class";
+import { ajax } from "discourse/lib/ajax";
import cookie, { removeCookie } from "discourse/lib/cookie";
import { i18n } from "discourse-i18n";
@@ -11,6 +12,7 @@ const SHOW_ORIGINAL_COOKIE = "content-localization-show-original";
const SHOW_ORIGINAL_COOKIE_EXPIRY = 30;
export default class TopicLocalizedContentToggle extends Component {
+ @service currentUser;
@service router;
@service toasts;
@@ -18,18 +20,31 @@ export default class TopicLocalizedContentToggle extends Component {
constructor() {
super(...arguments);
- this.showingOriginal = cookie(SHOW_ORIGINAL_COOKIE);
+ if (this.currentUser) {
+ this.showingOriginal =
+ this.currentUser.user_option?.show_original_content;
+ } else {
+ this.showingOriginal = cookie(SHOW_ORIGINAL_COOKIE);
+ }
}
@action
async showOriginal() {
- if (this.showingOriginal) {
- removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });
- } else {
+ const newValue = !this.showingOriginal;
+
+ if (this.currentUser) {
+ this.currentUser.set("user_option.show_original_content", newValue);
+ ajax(`/u/${this.currentUser.username}.json`, {
+ type: "PUT",
+ data: { show_original_content: newValue },
+ });
+ } else if (newValue) {
cookie(SHOW_ORIGINAL_COOKIE, true, {
path: "/",
expires: SHOW_ORIGINAL_COOKIE_EXPIRY,
});
+ } else {
+ removeCookie(SHOW_ORIGINAL_COOKIE, { path: "/" });
}
const toastKey = this.showingOriginal
diff --git a/frontend/discourse/app/controllers/preferences/interface.js b/frontend/discourse/app/controllers/preferences/interface.js
index e2ae140294b..8a97f37ec79 100644
--- a/frontend/discourse/app/controllers/preferences/interface.js
+++ b/frontend/discourse/app/controllers/preferences/interface.js
@@ -74,6 +74,7 @@ export default class InterfaceController extends Controller {
"bookmark_auto_delete_preference",
"interface_color_mode",
"enable_markdown_monospace_font",
+ "show_original_content",
];
if (this.makeThemeDefault) {
diff --git a/frontend/discourse/app/templates/preferences/interface.gjs b/frontend/discourse/app/templates/preferences/interface.gjs
index 6b31e4e87c5..ff8a7386eb8 100644
--- a/frontend/discourse/app/templates/preferences/interface.gjs
+++ b/frontend/discourse/app/templates/preferences/interface.gjs
@@ -222,6 +222,14 @@ export default <template>
data-setting-name="user-enable-markdown-monospace-font"
class="pref-enable-markdown-monospace-font"
/>
+ {{#if @controller.siteSettings.content_localization_enabled}}
+ <PreferenceCheckbox
+ @labelKey="user.show_original_content"
+ @checked={{@controller.model.user_option.show_original_content}}
+ data-setting-name="user-show-original-content"
+ class="pref-show-original-content"
+ />
+ {{/if}}
<div
class="controls controls-dropdown pref-page-title"
data-setting-name="user-page-title"
diff --git a/lib/content_localization.rb b/lib/content_localization.rb
index b606c4f226f..157142bb2ed 100644
--- a/lib/content_localization.rb
+++ b/lib/content_localization.rb
@@ -6,6 +6,7 @@ class ContentLocalization
# @param scope [Object] The serializer scope from which the method is called
# @return [Boolean] if the cookie is set, false otherwise
def self.show_original?(scope)
+ return true if scope&.user&.user_option&.show_original_content
scope&.request&.cookies&.key?(SHOW_ORIGINAL_COOKIE)
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js
index 4405d36e747..3ce954af888 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/translation.js
@@ -11,7 +11,12 @@ export default apiInitializer((api) => {
api.registerCustomPostMessageCallback(
"localized",
(topicController, data) => {
- if (!cookie("content-localization-show-original")) {
+ const currentUser = api.getCurrentUser();
+ const showOriginal =
+ currentUser?.user_option?.show_original_content ||
+ cookie("content-localization-show-original");
+
+ if (!showOriginal) {
const postStream = topicController.get("model.postStream");
postStream.triggerChangedPost(data.id, data.updated_at);
}
diff --git a/spec/lib/content_localization_spec.rb b/spec/lib/content_localization_spec.rb
index ee05f0c488d..7a391efc77a 100644
--- a/spec/lib/content_localization_spec.rb
+++ b/spec/lib/content_localization_spec.rb
@@ -1,12 +1,15 @@
# frozen_string_literal: true
describe ContentLocalization do
- def create_scope(cookie: nil)
+ def create_scope(cookie: nil, user: nil)
env = create_request_env.merge("HTTP_COOKIE" => cookie)
- mock().tap { |m| m.stubs(:request).returns(ActionDispatch::Request.new(env)) }
+ mock().tap do |m|
+ m.stubs(:request).returns(ActionDispatch::Request.new(env))
+ m.stubs(:user).returns(user)
+ end
end
- describe ".show_original" do
+ describe ".show_original?" do
it "returns true when cookie is present" do
scope = create_scope(cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
@@ -18,6 +21,28 @@ describe ContentLocalization do
expect(ContentLocalization.show_original?(scope)).to be false
end
+
+ it "returns true when user preference is set" do
+ user = Fabricate(:user)
+ user.user_option.update!(show_original_content: true)
+ scope = create_scope(user: user)
+
+ expect(ContentLocalization.show_original?(scope)).to be true
+ end
+
+ it "returns false when user preference is not set and no cookie" do
+ user = Fabricate(:user)
+ scope = create_scope(user: user)
+
+ expect(ContentLocalization.show_original?(scope)).to be false
+ end
+
+ it "returns true when user preference is not set but cookie is present" do
+ user = Fabricate(:user)
+ scope = create_scope(user: user, cookie: ContentLocalization::SHOW_ORIGINAL_COOKIE)
+
+ expect(ContentLocalization.show_original?(scope)).to be true
+ end
end
describe ".show_translated_post?" do
diff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.json
index e21ead93da2..f2a2ad74375 100644
--- a/spec/requests/api/schemas/json/user_get_response.json
+++ b/spec/requests/api/schemas/json/user_get_response.json
@@ -830,6 +830,9 @@
},
"interface_color_mode": {
"type": "integer"
+ },
+ "show_original_content": {
+ "type": "boolean"
}
},
"required": [
@@ -871,7 +874,8 @@
"timezone",
"skip_new_user_tips",
"topics_unread_when_closed",
- "interface_color_mode"
+ "interface_color_mode",
+ "show_original_content"
]
}
},
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 7ecf4559d77..cd66ad3a7e8 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -99,6 +99,22 @@ describe "Content Localization" do
topic_list.visit_topic_with_title("Life strategies from The Art of War")
end
+ it "persists 'Show Original' preference for logged-in users across page loads" do
+ sign_in(japanese_user)
+
+ visit("/t/#{topic.id}")
+ expect(topic_page.has_topic_title?("孫子兵法からの人生戦略")).to eq(true)
+
+ page.find(TOGGLE_LOCALIZE_BUTTON_SELECTOR).click
+ expect(topic_page.has_topic_title?("Life strategies from The Art of War")).to eq(true)
+
+ # preference persists after full page reload
+ visit("/t/#{topic.id}")
+ expect(topic_page.has_topic_title?("Life strategies from The Art of War")).to eq(true)
+
+ expect(japanese_user.reload.user_option.show_original_content).to eq(true)
+ end
+
it "allows users to set their post's locale when posting" do
sign_in(japanese_user)