Sha256: c1b6154309f0780273907028fd15fcfb8964269c33ac27697ad4abb2842c925c

Contents?: true

Size: 886 Bytes

Versions: 1

Compression:

Stored size: 886 Bytes

Contents

require 'rack/accept'

module Rack
  class LocaleRootRedirect
    def initialize(app, locales = {})
      @locales = locales
      @available_locales = locales.keys.map(&:to_s)
      @default_locale = @available_locales.first
      @app = app
    end

    def call(env)
      status, headers, response = @app.call(env)

      if root_request?(env)
        language_matcher = env['rack-accept.request'].language.tap { |m| m.first_level_match = true }
        redirect_lang = language_matcher.best_of(@available_locales) || @default_locale

        status = 302
        query_string = env['QUERY_STRING'] ? "?#{env['QUERY_STRING']}" : ''
        headers['Location'] = @locales[redirect_lang.to_sym] + query_string
      end

      [status, headers, response]
    end

  protected

    def root_request?(env)
      %r[\A/(?<query_string>\?.*|\Z)].match(env['PATH_INFO'])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-locale-root-redirect-0.2.1 lib/rack/locale_root_redirect/middleware.rb