2015-12-31 18:33:35 -05:00
|
|
|
# frozen_string_literal: true
|
2014-08-16 13:32:03 -04:00
|
|
|
#
|
|
|
|
# Simple middleware to save the current locale and restore it when the job executes.
|
|
|
|
# Use it by requiring it in your initializer:
|
|
|
|
#
|
|
|
|
# require 'sidekiq/middleware/i18n'
|
|
|
|
#
|
2013-03-03 19:10:50 -05:00
|
|
|
module Sidekiq::Middleware::I18n
|
|
|
|
# Get the current locale and store it in the message
|
|
|
|
# to be sent to Sidekiq.
|
|
|
|
class Client
|
2014-03-26 00:38:17 -04:00
|
|
|
def call(worker_class, msg, queue, redis_pool)
|
2013-06-19 11:33:28 -04:00
|
|
|
msg['locale'] ||= I18n.locale
|
2013-03-03 19:10:50 -05:00
|
|
|
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
|
2014-01-01 17:01:50 -05:00
|
|
|
I18n.locale = I18n.default_locale
|
2013-03-03 19:10:50 -05:00
|
|
|
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
|