class Locomotive::Steam::Middlewares::LocaleRedirection

Redirect to the same page with or without the locale in the url based on the “prefix_default_locale” property of the current site.

See the specs (spec/unit/middlewares/locale_redirection_spec.rb) for more details.

Public Instance Methods

_call() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 13
def _call
  if redirect_to_root_path_with_lang
    redirect_to(path_with_locale, 302)
  elsif url = redirect_url
    redirect_to(url, redirect_type)
  end
end

Protected Instance Methods

apply_redirection?() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 40
def apply_redirection?
  site.locales.size > 1 && request.get? && env['PATH_INFO'] != '/sitemap.xml'
end
default_locale?() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 44
def default_locale?
  locale.to_s == site.default_locale.to_s
end
locale_mentioned_in_path?() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 48
def locale_mentioned_in_path?
  env['steam.locale_in_path']
end
locale_not_mentioned_in_path?() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 52
def locale_not_mentioned_in_path?
  !locale_mentioned_in_path?
end
path_with_locale() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 56
def path_with_locale
  modify_path do |segments|
    segments.insert(1, locale)
  end
end
redirect_to_root_path_with_lang() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 62
def redirect_to_root_path_with_lang
  locale_not_mentioned_in_path? && path.gsub(/^\//, '') == '' && !default_locale?
end
redirect_type() click to toggle source

only applied if redirect_url is not nil

# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 34
def redirect_type
  # We don't want a permanent redirection for the index page in case
  # the user wants to change the current locale from the index page.
  self.path == '/' && self.locales.size > 1 ? 302 : 301
end
redirect_url() click to toggle source
# File lib/locomotive/steam/middlewares/locale_redirection.rb, line 23
def redirect_url
  if apply_redirection?
    if site.prefix_default_locale
      path_with_locale if locale_not_mentioned_in_path?
    else
      modify_path(env['steam.path']) if default_locale? && locale_mentioned_in_path?
    end
  end
end