Class: Locomotive::API::Middlewares::LocaleMiddleware

Inherits:
Object
  • Object
show all
Defined in:
app/api/locomotive/api/middlewares/locale_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ LocaleMiddleware

Returns a new instance of LocaleMiddleware.


7
8
9
# File 'app/api/locomotive/api/middlewares/locale_middleware.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Rules (order is important):

  1. check for the X-Locomotive-Locale header

  2. check for the locale request param

  3. check for the site default locale

  4. if none, take the default locomotive locale


18
19
20
21
22
23
24
# File 'app/api/locomotive/api/middlewares/locale_middleware.rb', line 18

def call(env)
  locale = find_locale(env)
  setup_i18n_fallback(env['locomotive.site'])
  ::Mongoid::Fields::I18n.with_locale(locale) do
    @app.call(env)
  end
end