Sha256: b14e00a39881eb22c70e65f4018400b62d86ccf16925fa26ff753a0ef92dbd01

Contents?: true

Size: 725 Bytes

Versions: 1

Compression:

Stored size: 725 Bytes

Contents

require "rack/accept"

module Rack
  class LocaleRootRedirect
    VERSION = "0.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 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 = 301
        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.1 lib/rack/locale-root-redirect.rb