Skip to content

Commit ccb7464

Browse files
committed
Include the actual ActiveJob locale when serializing rather than I18n.locale
1 parent c1689d8 commit ccb7464

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

activejob/lib/active_job/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def serialize
114114
"arguments" => serialize_arguments_if_needed(arguments),
115115
"executions" => executions,
116116
"exception_executions" => exception_executions,
117-
"locale" => I18n.locale.to_s,
117+
"locale" => locale || I18n.locale.to_s,
118118
"timezone" => timezone,
119119
"enqueued_at" => Time.now.utc.iso8601(9),
120120
"scheduled_at" => scheduled_at ? scheduled_at.utc.iso8601(9) : nil,

activejob/test/cases/job_serialization_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ class JobSerializationTest < ActiveSupport::TestCase
2121
assert_equal "en", HelloJob.new.serialize["locale"]
2222
end
2323

24+
test "a deserialized job keeps its locale even if I18n.locale changes" do
25+
old_locales = I18n.available_locales
26+
begin
27+
I18n.available_locales = [:en, :es]
28+
I18n.locale = :es
29+
payload = HelloJob.new.serialize
30+
assert_equal "es", payload["locale"]
31+
32+
I18n.locale = :en
33+
34+
new_job = HelloJob.new
35+
new_job.deserialize(payload)
36+
37+
assert_equal "es", new_job.serialize["locale"]
38+
ensure
39+
I18n.available_locales = old_locales
40+
end
41+
end
42+
2443
test "serialize and deserialize are symmetric" do
2544
# Ensure `enqueued_at` does not change between serializations
2645
freeze_time

0 commit comments

Comments
 (0)