-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make ActiveJob locale aware #20800
Make ActiveJob locale aware #20800
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module ActiveJob | ||
module Translation #:nodoc: | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around_perform do |job, block, _| | ||
I18n.with_locale(job.locale, &block) | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'helper' | ||
require 'jobs/translated_hello_job' | ||
|
||
class TranslationTest < ActiveSupport::TestCase | ||
setup do | ||
JobBuffer.clear | ||
I18n.available_locales = [:en, :de] | ||
@job = TranslatedHelloJob.new('Johannes') | ||
end | ||
|
||
teardown do | ||
I18n.available_locales = [:en] | ||
end | ||
|
||
test 'it performs the job in the given locale' do | ||
@job.locale = :de | ||
@job.perform_now | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry if I'm missing something but since the original issue was on However with the following: test 'it performs the job in the given locale' do
I18n.locale = :de
TranslatedHelloJob.perform_later('Johannes')
assert_equal "Johannes says Guten Tag", JobBuffer.last_value
end the only failure is with Sucker Punch on my machine without your patch ; that seems strange. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @robin850 in the regular tests adapters are set to inline mode so when perform_later is called the adapters are executing the job immediately. SP probably still forks in inline mode. We did had some integration tests which starts adapters workers and I would test in there that the locale is set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @robin850 Thanks for having a look at it. From my point of view the Could you outline which test you'd like to see? I'm happy to add it then! :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep I agree, the test does a good job, we should keep it. My comment was just about adding a regression test for the case you're describing in your commit message (i.e. with test 'current I18n.locale is kept running #perform_later' do
skip if adapter_is?(:inline)
I18n.locale = :de
TranslatedHelloJob.perform_later('Johannes')
wait_for_jobs_to_finish_for(5.seconds)
assert_equal "Johannes says Guten Tag", JobBuffer.last_value
end What do you think ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xijo be aware that JobBuffer won't work on the integration tests as the jobs are executed on another process. You can use the existing system in place to write content to a file(see activejob/test/support/integration/dummy_app_template.rb, activejob/test/support/integration/test_case_helpers.rb, activejob/test/integration/queuing_test.rb) |
||
assert_equal "Johannes says Guten Tag", JobBuffer.last_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Way easier to spot 👍 |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require_relative '../support/job_buffer' | ||
|
||
class TranslatedHelloJob < ActiveJob::Base | ||
def perform(greeter = "David") | ||
translations = { en: 'Hello', de: 'Guten Tag' } | ||
hello = translations[I18n.locale] | ||
|
||
JobBuffer.add("#{greeter} says #{hello}") | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,17 @@ | |
JobsManager.current_manager.setup | ||
CODE | ||
|
||
initializer 'i18n.rb', <<-CODE | ||
I18n.available_locales = [:en, :de] | ||
CODE | ||
|
||
file 'app/jobs/test_job.rb', <<-CODE | ||
class TestJob < ActiveJob::Base | ||
queue_as :integration_tests | ||
|
||
def perform(x) | ||
File.open(Rails.root.join("tmp/\#{x}"), "w+") do |f| | ||
f.write x | ||
f.write I18n.locale | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kaspth I added the integration test and used the fact that the content of the file wasn't asserted before. I thought that might be slightly better than adding a whole new job for this one test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as it doesn't break other tests, this could probably work. What do you think, @cristianbica @robin850? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's ok. We're not asserting the content of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool 👍 |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,6 +280,19 @@ UserMailer.welcome(@user).deliver_later | |
``` | ||
|
||
|
||
Internationalization | ||
-------------------- | ||
|
||
Each job uses the `I18n.locale` set when the job was created. Useful if you send | ||
emails asynchronously: | ||
|
||
```ruby | ||
I18n.locale = :eo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a newline after this. |
||
|
||
UserMailer.welcome(@user).deliver_later # Email will be localized to Esparanto. | ||
``` | ||
|
||
|
||
GlobalID | ||
-------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you think that this is too general name? I'm currently upgrading to Rails 4.2.4 and getting errors like:
In my job, I have a private method
locale
definedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it'll be tough to fix until another release. To me locale sounds like the most appropriate name.