Skip to content

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

FIX: Do not change language for logged in user when tl is present (#35942)

Natalie Tay0ba34631200b
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 38dee81e823..af37a8d0f90 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -431,6 +431,7 @@ class ApplicationController < ActionController::Base
else
locale = Discourse.anonymous_locale(request)
locale ||= SiteSetting.default_locale
+ persist_locale_param_to_cookie
end
locale = SiteSettings::DefaultsProvider::DEFAULT_LOCALE if !I18n.locale_available?(locale)
@@ -1073,4 +1074,15 @@ class ApplicationController < ActionController::Base
ensure
dont_cache_page
end
+
+ def persist_locale_param_to_cookie
+ if SiteSetting.set_locale_from_param && SiteSetting.set_locale_from_cookie &&
+ (locale_param = params[Discourse::LOCALE_PARAM]).present?
+ if I18n.locale_available?(locale_param)
+ cookie_args = { path: "/" }
+ cookie_args[:path] = Discourse.base_path if Discourse.base_path.present?
+ cookies[:locale] = cookie_args.merge(value: locale_param)
+ end
+ end
+ end
end
diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb
index 32cb3d9b50d..7399094cb99 100644
--- a/app/helpers/topics_helper.rb
+++ b/app/helpers/topics_helper.rb
@@ -24,11 +24,13 @@ module TopicsHelper
def localize_topic_view_content(topic_view)
return if cookies.key?(ContentLocalization::SHOW_ORIGINAL_COOKIE)
- crawl_locale = params[Discourse::LOCALE_PARAM].presence || SiteSetting.default_locale
+ # locale param is appropriately set in the application controller
+ # depending on site settings and presence of user
+ locale = I18n.locale
- LocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, crawl_locale)
+ LocalizationAttributesReplacer.replace_topic_attributes(topic_view.topic, locale)
topic_view.posts.each do |post|
- LocalizationAttributesReplacer.replace_post_attributes(post, crawl_locale)
+ LocalizationAttributesReplacer.replace_post_attributes(post, locale)
end
end
end
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index b877f7dfbbc..77eaed1a18d 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -1327,25 +1327,84 @@ RSpec.describe ApplicationController do
end
end
- context "with set_locale_from_param enabled" do
+ context "with set_locale_from_param" do
context "when param locale differs from default locale" do
before do
SiteSetting.allow_user_locale = true
- SiteSetting.set_locale_from_param = true
SiteSetting.default_locale = "en"
end
context "with an anonymous user" do
it "uses the locale from the param" do
+ SiteSetting.set_locale_from_param = true
+
get "/latest?tl=es"
expect(response.status).to eq(200)
expect(main_locale_scripts(response.body)).to contain_exactly("es")
expect(I18n.locale.to_s).to eq(SiteSettings::DefaultsProvider::DEFAULT_LOCALE) # doesn't leak after requests
end
+
+ it "sets a cookie with the locale from the param for persistence" do
+ SiteSetting.set_locale_from_param = false
+ SiteSetting.set_locale_from_cookie = false
+ get "/latest?tl=ja"
+ expect(response.status).to eq(200)
+ expect(response.cookies["locale"]).to eq(nil)
+
+ SiteSetting.set_locale_from_param = true
+ SiteSetting.set_locale_from_cookie = false
+ get "/latest?tl=ja"
+ expect(response.status).to eq(200)
+ expect(response.cookies["locale"]).to eq(nil)
+
+ SiteSetting.set_locale_from_param = true
+ SiteSetting.set_locale_from_cookie = true
+ get "/latest?tl=ja"
+ expect(response.status).to eq(200)
+ expect(response.cookies["locale"]).to eq("ja")
+ end
+
+ it "does not set a cookie for invalid locales" do
+ SiteSetting.set_locale_from_param = true
+ SiteSetting.set_locale_from_cookie = true
+
+ get "/latest?tl=invalid_locale"
+ expect(response.status).to eq(200)
+ expect(response.cookies["locale"]).to be_nil
+ end
+
+ it "persists locale across requests via cookie" do
+ SiteSetting.set_locale_from_param = true
+ SiteSetting.set_locale_from_cookie = true
+
+ get "/latest?tl=ja"
+ expect(response.status).to eq(200)
+ expect(response.cookies["locale"]).to eq("ja")
+ expect(main_locale_scripts(response.body)).to contain_exactly("ja")
+
+ # next request without tl parameter should use the cookie
+ get "/latest", headers: { Cookie: "locale=ja" }
+ expect(response.status).to eq(200)
+ expect(main_locale_scripts(response.body)).to contain_exactly("ja")
+ end
+ end
+
+ context "with a logged-in user" do
+ fab!(:user) { Fabricate(:user, locale: "de") }
+
+ it "ignores the tl parameter and uses user's preference" do
+ sign_in(user)
+ get "/latest?tl=es"
+ expect(response.status).to eq(200)
+ expect(main_locale_scripts(response.body)).to contain_exactly("de")
+ expect(response.cookies["locale"]).to be_nil
+ end
end
context "when the preferred locale includes a region" do
it "returns the locale and region separated by an underscore" do
+ SiteSetting.set_locale_from_param = true
+
get "/latest?tl=zh-CN"
expect(response.status).to eq(200)
expect(main_locale_scripts(response.body)).to contain_exactly("zh_CN")
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index bd4ff7a6c5b..33257c2182b 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -5777,6 +5777,8 @@ RSpec.describe TopicsController do
before do
SiteSetting.content_localization_enabled = true
+ SiteSetting.allow_user_locale = true
+ SiteSetting.set_locale_from_param = true
topic.update!(category: subcategory, tags: [tag], locale: "en")
topic.first_post.update(locale: "en")
diff --git a/spec/system/content_localization_spec.rb b/spec/system/content_localization_spec.rb
index 46c3adb7ecd..26278ac22dc 100644
--- a/spec/system/content_localization_spec.rb
+++ b/spec/system/content_localization_spec.rb
@@ -133,6 +133,37 @@ describe "Content Localization" do
expect(post_3_obj.post).to have_content("A general is one who ..")
end
+ context "with tl parameter" do
+ before do
+ SiteSetting.set_locale_from_param = true
+ SiteSetting.set_locale_from_cookie = true
+ end
+
+ fab!(:topic2) do
+ topic = Fabricate(:topic, title: "The Life of Oda Nobunaga", locale: "en", user: admin)
+ Fabricate(:post, topic:, locale: "en", raw: "Oda Nobunaga was a powerful daimyo ...")
+ topic
+ end
+ fab!(:topic_localization2) do
+ Fabricate(:topic_localization, topic: topic2, locale: "ja", fancy_title: "織田信長の生涯")
+ end
+
+ it "persists locale for anonymous users across page views" do
+ visit("/t/#{topic.id}?tl=ja")
+ expect(topic_page.topic_title).to have_content("孫子兵法からの人生戦略")
+
+ visit("/t/#{topic2.id}")
+ expect(topic_page.topic_title).to have_content("織田信長の生涯")
+ end
+
+ it "ignores tl parameter for logged-in users" do
+ sign_in(site_local_user)
+ visit("/t/#{topic.id}?tl=ja")
+
+ expect(topic_page.has_topic_title?("Life strategies from The Art of War")).to eq(true)
+ end
+ end
+
context "when editing" do
let(:edit_localized_post_dialog) { PageObjects::Components::Dialog.new }
let(:fast_editor) { PageObjects::Components::FastEditor.new }
@@ -200,7 +231,7 @@ describe "Content Localization" do
end
end
- context "when loading 20+ posts in stream", trace: true do
+ context "when loading 20+ posts in stream" do
before do
highest = topic.highest_post_number
22.times do |i|
diff --git a/spec/system/page_objects/pages/topic.rb b/spec/system/page_objects/pages/topic.rb
index 6f855c1a981..84f58b5e854 100644
--- a/spec/system/page_objects/pages/topic.rb
+++ b/spec/system/page_objects/pages/topic.rb
@@ -39,6 +39,10 @@ module PageObjects
::Topic.find(current_topic_id)
end
+ def topic_title
+ find("#topic-title .fancy-title")
+ end
+
def has_topic_title?(text)
has_css?("h1 .fancy-title", text: text)
end