Class: Rack::UserLocale

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ UserLocale

Returns a new instance of UserLocale.



6
7
8
9
10
# File 'lib/rack-user-locale.rb', line 6

def initialize(app, options = {})
  @app, @options = app, {
      :accepted_locales => []
    }.merge(options)
end

Instance Method Details

#call(env) ⇒ Object

The “non_ruby_request” variable denotes a request which doesn’t call a Ruby script. This might be a CSS, static HTML, image, media, or JavaScript request.

This check is in place so that if you’re using this gem with multiple Rack apps running in the same application context, you won’t have static resources resetting the currency selection due to requests going into the main app instead of a sub-app.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack-user-locale.rb', line 20

def call(env)
  @env = env
  @request = Rack::Request.new(@env)
  set_locale

  non_ruby_request = (@env["SCRIPT_NAME"] == nil || @env["SCRIPT_NAME"] == "")
  if @request.post? || @request.put? || @request.delete? || non_ruby_request
    @app.call(env)
  else
    status, headers, body = @app.call(@env)
    response = Rack::Response.new(body, status, headers)
    response.set_cookie("user-locale", {
      :value => I18n.locale,
      :path => "/",
      :domain => @request.host}) if get_cookie_locale != I18n.locale.to_s
    response.finish
  end
end