Class: Rack::R18n
- Inherits:
-
Object
- Object
- Rack::R18n
- Defined in:
- lib/rack-r18n.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Avaible options: :default => en :place => “i18n” Note: This is relative to #root.
-
#place ⇒ Object
readonly
Avaible options: :default => en :place => “i18n” Note: This is relative to #root.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
- #generate_r18n ⇒ Object
-
#initialize(app, options = {}) ⇒ R18n
constructor
A new instance of R18n.
- #session_locale(session, locales, locale = nil) ⇒ Object
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, = {}) @app = app @options = ::R18n::I18n.default = @options[:default] || "en" @options[:place] ||= "i18n" @place = Array(@options[:place]).map do |dir| (Pathname(self.class.root) + dir). end end |
Instance Attribute Details
#options ⇒ Object (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 end |
#place ⇒ Object (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
.root ⇒ Object
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 |
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_r18n ⇒ Object
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 |