Sha256: 52152ace38f75b1b28782dab662210de070be0c55b97dd97be44fb02b7db9b78

Contents?: true

Size: 725 Bytes

Versions: 1

Compression:

Stored size: 725 Bytes

Contents

require "rack/accept"

module Rack
  class LocaleRootRedirect
    VERSION = "0.0.4"

    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 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]
      end

      [status, headers, response]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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