Class: Locomotive::Middlewares::Locale

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

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Locale

Returns a new instance of Locale.



5
6
7
# File 'lib/locomotive/middlewares/locale.rb', line 5

def initialize(app, opts = {})
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
# File 'lib/locomotive/middlewares/locale.rb', line 9

def call(env)
  retrieve_and_set_locale(env)
  @app.call(env)
end

#retrieve_and_set_locale(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/locomotive/middlewares/locale.rb', line 14

def retrieve_and_set_locale(env)
  site = env['locomotive.site']

  if site.try(:localized?)
    if env['PATH_INFO'] =~ %r{^/(#{site.locales.join('|')})+(\/|$)}
      locale  = $1
      path    = env['PATH_INFO'].gsub($1 + $2, '').gsub(/(\/_edit|\/_admin)$/, '')

      Locomotive.log "[extract locale] locale = #{locale} / #{path}"

      env['locomotive.locale']  = locale
      env['locomotive.path']    = path
    end
  end
end