Class: Gluttonberg::Middleware::Locales

Inherits:
Object
  • Object
show all
Defined in:
lib/gluttonberg/middleware/locales.rb

Instance Method Summary (collapse)

Constructor Details

- (Locales) initialize(app)

A new instance of Locales



4
5
6
# File 'lib/gluttonberg/middleware/locales.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) call(env)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gluttonberg/middleware/locales.rb', line 8

def call(env)
  path = env['PATH_INFO']
  unless path =~ /^#{Gluttonberg::Engine.config.admin_path}/ || path.start_with?("/stylesheets")  || path.start_with?("/javascripts")   || path.start_with?("/images") ||  path.start_with?("/gluttonberg")  || path.start_with?("/asset") 
    case Gluttonberg::Engine.config.identify_locale
      when :subdomain
        # return the sub-domain
      when :prefix
        if Gluttonberg.localized?
          locale = path.split('/')[1]
          if locale.blank?
            result = Gluttonberg::Locale.first_default
          else  
            result = Gluttonberg::Locale.find_by_locale(locale)
          end
        else # take default locale
          result = Gluttonberg::Locale.first_default
          locale = result.slug               
        end  
        if result
          env['PATH_INFO'].gsub!("/#{locale}", '')
          env['gluttonberg.locale'] = result
        end
      when :domain
        env['SERVER_NAME']
    end
  end  
  @app.call(env)
end