Skip to content

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

FIX: Reports did not respect user locale (#30524)

Martin Brennan6b36b0b68d28
diff --git a/lib/hijack.rb b/lib/hijack.rb
index 1e123fd6fc3..fd92488aa0d 100644
--- a/lib/hijack.rb
+++ b/lib/hijack.rb
@@ -13,6 +13,10 @@ module Hijack
request.env["discourse.request_tracker.skip"] = true
request_tracker = request.env["discourse.request_tracker"]
+ # need this because we can't call with_resolved_locale with around_action
+ # when we are evaluating the block
+ resolved_locale = I18n.locale
+
# in the past unicorn would recycle env, this is not longer the case
env = request.env
@@ -61,7 +65,7 @@ module Hijack
view_start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
- instance.instance_eval(&blk)
+ I18n.with_locale(resolved_locale) { instance.instance_eval(&blk) }
rescue => e
# TODO we need to reuse our exception handling in ApplicationController
Discourse.warn_exception(
diff --git a/spec/lib/hijack_spec.rb b/spec/lib/hijack_spec.rb
index 041cdfd1de6..0775c9372bd 100644
--- a/spec/lib/hijack_spec.rb
+++ b/spec/lib/hijack_spec.rb
@@ -5,6 +5,7 @@ RSpec.describe Hijack do
attr_reader :io
include Hijack
+ include CurrentUser
def initialize(env = {})
@io = StringIO.new
@@ -232,4 +233,34 @@ RSpec.describe Hijack do
expect(tester.response.status).to eq(503)
end
+
+ context "when there is a current user" do
+ fab!(:test_current_user) { Fabricate(:user) }
+
+ it "captures the current user" do
+ test_user_id = nil
+
+ tester =
+ Hijack::Tester.new(Auth::DefaultCurrentUserProvider::CURRENT_USER_KEY => test_current_user)
+
+ tester.hijack_test { test_user_id = current_user.id }
+
+ expect(test_user_id).to eq(test_current_user.id)
+ end
+
+ it "uses the current user's locale for translations" do
+ SiteSetting.allow_user_locale = true
+ test_current_user.update!(locale: "es")
+ test_translation = nil
+
+ tester =
+ Hijack::Tester.new(Auth::DefaultCurrentUserProvider::CURRENT_USER_KEY => test_current_user)
+
+ # Simulates the around_action that sets the locale in ApplicationController, since this is
+ # not a request spec.
+ tester.with_resolved_locale { tester.hijack_test { test_translation = I18n.t("topics") } }
+
+ expect(test_translation).to eq(I18n.t("topics", locale: "es"))
+ end
+ end
end
diff --git a/spec/requests/admin/reports_controller_spec.rb b/spec/requests/admin/reports_controller_spec.rb
index 3a6b1a57f16..e0965382057 100644
--- a/spec/requests/admin/reports_controller_spec.rb
+++ b/spec/requests/admin/reports_controller_spec.rb
@@ -56,8 +56,9 @@ RSpec.describe Admin::ReportsController do
before { sign_in(admin) }
context "with valid params" do
+ fab!(:topic)
+
it "renders the reports as JSON" do
- Fabricate(:topic)
get "/admin/reports/bulk.json",
params: {
reports: {
@@ -73,6 +74,30 @@ RSpec.describe Admin::ReportsController do
expect(response.status).to eq(200)
expect(response.parsed_body["reports"].count).to eq(2)
end
+
+ it "uses the user's locale for report names and descriptions" do
+ SiteSetting.allow_user_locale = true
+ admin.update!(locale: "es")
+ get "/admin/reports/bulk.json",
+ params: {
+ reports: {
+ topics: {
+ limit: 10,
+ },
+ likes: {
+ limit: 10,
+ },
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["reports"].first["title"]).to eq(
+ I18n.t("reports.topics.title", locale: "es"),
+ )
+ expect(response.parsed_body["reports"].first["description"]).to eq(
+ I18n.t("reports.topics.description", locale: "es"),
+ )
+ end
end
context "with invalid params" do