Class: Er18Ern::LocaleCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/er18ern.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ LocaleCookie

Returns a new instance of LocaleCookie.



4
5
6
# File 'lib/er18ern.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/er18ern.rb', line 8

def call(env)
  request = Rack::Request.new(env)
  request_locale = request.params["locale"] || request.cookies["eylocale"]
  if request_locale && I18n.available_locales.include?(request_locale.to_sym)
    I18n.locale = request_locale
  else
    I18n.locale = I18n.default_locale
  end
  status, headers, body = @app.call(env)
  cookie = {:value => I18n.locale, :expires => Time.now + 10.days, :domain => request.host.gsub(/^[^\.]*/,""), :path => "/"}
  Rack::Utils.set_cookie_header!(headers, "eylocale", cookie)
  [status, headers, body]
end