Class: Rack::R18n

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of R18n.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rack-r18n.rb', line 11

def initialize(app, options = {})
  @app = app
  @options = options

  ::R18n::I18n.default = @options[:default] || "en"
  @options[:place] ||= "i18n"
  @place = Array(@options[:place]).map do |dir|
    (Pathname(self.class.root) + dir).expand_path
  end
end

Instance Attribute Details

#optionsObject (readonly)

Avaible options: :default => en :place => “i18n” Note: This is relative to #root



10
11
12
# File 'lib/rack-r18n.rb', line 10

def options
  @options
end

#placeObject (readonly)

Avaible options: :default => en :place => “i18n” Note: This is relative to #root



10
11
12
# File 'lib/rack-r18n.rb', line 10

def place
  @place
end

Class Method Details

.rootObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rack-r18n.rb', line 48

def self.root
  @@root ||=  if defined? Merb
                Merb.root
              elsif defined? Sinatra
                Sinatra.root
              elsif defined? Rails
                Rails.root
              elsif defined? Rango
                Rango.root
              else
                Dir.pwd
              end
end

.root=(root) ⇒ Object



62
63
64
# File 'lib/rack-r18n.rb', line 62

def self.root=(root)
  @@root = root
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
# File 'lib/rack-r18n.rb', line 22

def call(env)
  @env = env
  ::R18n.thread_set {generate_r18n}
  @app.call(@env)
end

#generate_r18nObject



28
29
30
31
32
33
34
35
# File 'lib/rack-r18n.rb', line 28

def generate_r18n
  request = Rack::Request.new(@env)
  locales = ::R18n::I18n.parse_http(@env['HTTP_ACCEPT_LANGUAGE'])
  if locale = session_locale(@env['rack.session'], locales, request.params[:locale])
    locale.insert(0, locale)
  end
  ::R18n::I18n.new(locales, @place)
end

#session_locale(session, locales, locale = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/rack-r18n.rb', line 37

def session_locale(session, locales, locale=nil)
  if locale
    if session
      session[:locale] = locale
    end
  elsif session && session[:locale]
    locale = session[:locale]
  end
  locale
end