Sha256: 17c720ee2ef40c652fb37b2d0025a1a662d8dc6ee530f03b59452b9586c24fc6

Contents?: true

Size: 1.04 KB

Versions: 42

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true
#
# 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'
#
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, redis_pool)
      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 = I18n.default_locale
    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

Version data entries

42 entries across 42 versions & 3 rubygems

Version Path
sidekiq-5.2.10 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.9 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.8 lib/sidekiq/middleware/i18n.rb
sidekiq_cleaner-5.3.8 lib/sidekiq/middleware/i18n.rb
sidekiq_cleaner-5.3.6 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.7 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.6 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.5 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.4 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.3 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.2 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.1 lib/sidekiq/middleware/i18n.rb
sidekiq-5.2.0 lib/sidekiq/middleware/i18n.rb
sidekiq-5.1.3 lib/sidekiq/middleware/i18n.rb
sidekiq-5.1.2 lib/sidekiq/middleware/i18n.rb
sidekiq-5.1.1 lib/sidekiq/middleware/i18n.rb
sidekiq-5.1.0 lib/sidekiq/middleware/i18n.rb
sidekiq-5.0.5 lib/sidekiq/middleware/i18n.rb
sidekiq-5.0.4 lib/sidekiq/middleware/i18n.rb
sidekiq-5.0.3 lib/sidekiq/middleware/i18n.rb