Class: Rack::LocaleRootRedirect
- Inherits:
-
Object
- Object
- Rack::LocaleRootRedirect
- Defined in:
- lib/rack/locale_root_redirect/version.rb,
lib/rack/locale_root_redirect/middleware.rb
Constant Summary collapse
- VERSION =
'0.4'
- STATUS =
302
- ROOT_REQUEST_REGEX =
%r{\A/(?<query_string>\?.*|\Z)}
- RACK_ACCEPT_MISSING =
'Rack::LocaleRootRedirect must be used after Rack::Accept. Please make your application use Rack::Accept before Rack::LocaleRootRedirect.'
- REDIRECTED_RESPONSE_REGEX =
%r{\A3\d\d\Z}
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, locales = {}) ⇒ LocaleRootRedirect
constructor
A new instance of LocaleRootRedirect.
Constructor Details
#initialize(app, locales = {}) ⇒ LocaleRootRedirect
Returns a new instance of LocaleRootRedirect.
9 10 11 12 13 14 15 |
# File 'lib/rack/locale_root_redirect/middleware.rb', line 9 def initialize(app, locales = {}) @locales = locales @available_locales = locales.keys.map(&:to_s) @default_locale = @available_locales.first @app = app end |
Instance Method Details
#call(env) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack/locale_root_redirect/middleware.rb', line 18 def call(env) status, headers, response = @app.call(env) if should_redirect?(env, status) locale = best_locale(env) status = STATUS query_string = env['QUERY_STRING'] == '' ? '' : "?#{env['QUERY_STRING']}" headers['Vary'] = 'Accept-Language' headers['Location'] = @locales[locale.to_sym] + query_string end [status, headers, response] end |