Sha256: 37db503044a076851a68a565b63556212295334002c5713670543c254e4e6845

Contents?: true

Size: 1.07 KB

Versions: 43

Compression:

Stored size: 1.07 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
    include Sidekiq::ClientMiddleware
    def call(_jobclass, job, _queue, _redis)
      job["locale"] ||= I18n.locale
      yield
    end
  end

  # Pull the msg locale out and set the current thread to use it.
  class Server
    include Sidekiq::ServerMiddleware
    def call(_jobclass, job, _queue, &block)
      I18n.with_locale(job.fetch("locale", I18n.default_locale), &block)
    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

43 entries across 43 versions & 1 rubygems

Version Path
sidekiq-7.3.6 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.5 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.4 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.3 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.2 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.1 lib/sidekiq/middleware/i18n.rb
sidekiq-7.3.0 lib/sidekiq/middleware/i18n.rb
sidekiq-7.2.4 lib/sidekiq/middleware/i18n.rb
sidekiq-7.2.3 lib/sidekiq/middleware/i18n.rb
sidekiq-7.2.2 lib/sidekiq/middleware/i18n.rb
sidekiq-7.2.1 lib/sidekiq/middleware/i18n.rb
sidekiq-7.2.0 lib/sidekiq/middleware/i18n.rb
sidekiq-6.5.12 lib/sidekiq/middleware/i18n.rb
sidekiq-7.1.6 lib/sidekiq/middleware/i18n.rb
sidekiq-6.5.11 lib/sidekiq/middleware/i18n.rb
sidekiq-7.1.5 lib/sidekiq/middleware/i18n.rb
sidekiq-6.5.10 lib/sidekiq/middleware/i18n.rb
sidekiq-7.1.4 lib/sidekiq/middleware/i18n.rb
sidekiq-7.1.3 lib/sidekiq/middleware/i18n.rb
sidekiq-7.1.2 lib/sidekiq/middleware/i18n.rb