Class: Faalis::Middlewares::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/faalis/middlewares/locale.rb

Overview

Locale middleware for ‘Faalis`. This middleware try to set the locale from url.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Locale

Returns a new instance of Locale.



7
8
9
# File 'lib/faalis/middlewares/locale.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/faalis/middlewares/locale.rb', line 11

def call(env)
  req = Rack::Request.new(env)

  # Check whether locale is presence or not.
  if locale = req.path_info.match(/^([a-z]{2})\/.*/)
    # If its set the default locale to it.
    locale = locale[1]
  else
    # Use pre-defined default if it isn't
    locale = ::I18n.default_locale
  end

  req.params['locale'] ||= locale
  ::I18n.locale = locale
  @app.call env
end