Sha256: 20ae9ee57127c6ff5e18a6c2d64e226d7ad373ec228433c725f2455d9ca169dd

Contents?: true

Size: 796 Bytes

Versions: 1

Compression:

Stored size: 796 Bytes

Contents

require "rack/accept"

module Rack
  class LocaleRootRedirect
    VERSION = "0.1"

    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 match_data = %r[\A/(?<query_string>\?.*|\Z)].match(env["REQUEST_URI"])
        language_matcher = env['rack-accept.request'].language
        language_matcher.first_level_match = true
        redirect_lang = language_matcher.best_of(@available_locales) || @default_locale

        status = 302
        headers["Location"] = @locales[redirect_lang.to_sym] + match_data[:query_string]
      end

      [status, headers, response]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-locale-root-redirect-0.1 lib/rack/locale-root-redirect.rb