Sha256: 40a6065aad4bfbab9ddd8152e34700abf9cb4937b7bc045df14aceef6185ac9d

Contents?: true

Size: 892 Bytes

Versions: 1

Compression:

Stored size: 892 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.3 lib/rack/locale_root_redirect/middleware.rb