Sha256: 3201dc831dd8fe946e71eb9c8d713542883bbc8f0cf936779e71b2272d02590d

Contents?: true

Size: 742 Bytes

Versions: 17

Compression:

Stored size: 742 Bytes

Contents

require 'i18n'

module Rack
  class Locale
    def initialize(app)
      @app = app
    end

    def call(env)
      old_locale = I18n.locale
      locale = nil

      # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
      if lang = env["HTTP_ACCEPT_LANGUAGE"]
        lang = lang.split(",").map { |l|
          l += ';q=1.0' unless l =~ /;q=\d+\.\d+$/
          l.split(';q=')
        }.first
        locale = lang.first.split("-").first
      else
        locale = I18n.default_locale
      end

      locale = env['rack.locale'] = I18n.locale = locale.to_s
      status, headers, body = @app.call(env)
      headers['Content-Language'] = locale
      I18n.locale = old_locale
      [status, headers, body]
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
world-flags-0.6.6 lib/world_flags/rack_locale.rb
world-flags-0.6.5 lib/world_flags/rack_locale.rb
world-flags-0.6.4 lib/world_flags/rack_locale.rb
world-flags-0.6.3.1 lib/world_flags/rack_locale.rb
world-flags-0.6.3 lib/world_flags/rack_locale.rb
world-flags-0.6.2.1 lib/world_flags/rack_locale.rb
world-flags-0.6.2 lib/world_flags/rack_locale.rb
world-flags-0.6.1.1 lib/world_flags/rack_locale.rb
world-flags-0.6.1 lib/world_flags/rack_locale.rb
world-flags-0.6.0 lib/world_flags/rack_locale.rb
world-flags-0.5.1 lib/world_flags/rack_locale.rb
world-flags-0.5.0 lib/world_flags/rack_locale.rb
world-flags-0.4.9 lib/world_flags/rack_locale.rb
world-flags-0.4.8.3 lib/world_flags/rack_locale.rb
world-flags-0.4.8.2 lib/world_flags/rack_locale.rb
world-flags-0.4.7 lib/world_flags/rack_locale.rb
world-flags-0.4.5 lib/world_flags/rack_locale.rb