Class: Rack::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/locale.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Locale

Returns a new instance of Locale.



5
6
7
# File 'lib/rack/contrib/locale.rb', line 5

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack/contrib/locale.rb', line 9

def call(env)
  old_locale = I18n.locale

  begin
    locale = accept_locale(env) || I18n.default_locale
    locale = env['rack.locale'] = I18n.locale = locale.to_s
    status, headers, body = @app.call(env)
    headers['Content-Language'] = locale unless headers['Content-Language']
    [status, headers, body]
  ensure
    I18n.locale = old_locale
  end
end