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
3 changed files with 62 additions and 2 deletions

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