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

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