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

Our bulk report endpoint uses `hijack`, which does not
use the current user's locale via the `with_resolved_locale`
method in `ApplicationController`. This is happening because
we are doing `around_action` to set the locale, then calling
the code in the block inside the action directly when we use
`hijack`.

We can fix this by capturing `I18n.locale` when starting the
hijack then using `I18n.with_locale` when evaluating the
block inside `hijack`, this way the translations will always
use the correct locale based on the current user.
This commit is contained in:
Martin Brennan 2025-01-02 13:05:53 +10:00 committed by GitHub
parent 9a12eb5c3c
commit 6b36b0b68d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 2 deletions

View File

@ -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(

View File

@ -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

View File

@ -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