1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Add i18n middleware, fixes #750

This commit is contained in:
Mike Perham 2013-03-03 16:10:50 -08:00
parent d982cfd835
commit fe57c7d6dd
3 changed files with 62 additions and 0 deletions

View file

@ -1,6 +1,9 @@
2.8.0
-----------
- I18n support! Sidekiq can optionally save and restore the Rails locale
so it will be properly set when your jobs execute. Just include
`require 'sidekiq/middleware/i18n'` in your sidekiq initializer. [#750]
- Fix bug which could lose messages when using namespaces and the message
needs to be requeued in Redis. [#744]
- Refactor Redis namespace support [#747]. The redis namespace can no longer be

View file

@ -0,0 +1,35 @@
module Sidekiq::Middleware::I18n
# Get the current locale and store it in the message
# to be sent to Sidekiq.
class Client
def call(worker_class, msg, queue)
msg['locale'] = I18n.locale
yield
end
end
# Pull the msg locale out and set the current thread to use it.
class Server
def call(worker, msg, queue)
I18n.locale = msg['locale'] || I18n.default_locale
yield
ensure
I18n.locale = nil
end
end
end
Sidekiq.configure_client do |config|
config.client_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Client
end
end
Sidekiq.configure_server do |config|
config.client_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Client
end
config.server_middleware do |chain|
chain.add Sidekiq::Middleware::I18n::Server
end
end

View file

@ -99,4 +99,28 @@ class TestMiddleware < MiniTest::Unit::TestCase
assert_equal [], recorder
end
end
describe 'i18n' do
before do
require 'i18n'
require 'sidekiq/middleware/i18n'
end
it 'saves and restores locale' do
I18n.locale = 'fr'
msg = {}
mw = Sidekiq::Middleware::I18n::Client.new
mw.call(nil, msg, nil) { }
assert_equal :fr, msg['locale']
msg['locale'] = 'jp'
I18n.locale = nil
assert_equal :en, I18n.locale
mw = Sidekiq::Middleware::I18n::Server.new
mw.call(nil, msg, nil) do
assert_equal :jp, I18n.locale
end
assert_equal :en, I18n.locale
end
end
end