Sha256: 0c2e89c6fe0e9ed54b0cd3df660de4f884c2a53c4858b1a5149f2230cddcb69d

Contents?: true

Size: 1.79 KB

Versions: 13

Compression:

Stored size: 1.79 KB

Contents

module Locomotive::Steam
  module Middlewares

    # 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.
    #
    class LocaleRedirection < ThreadSafe

      include Concerns::Helpers

      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

      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

      # only applied if redirect_url is not nil
      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

      def apply_redirection?
        site.locales.size > 1 && request.get? && env['PATH_INFO'] != '/sitemap.xml'
      end

      def default_locale?
        locale.to_s == site.default_locale.to_s
      end

      def locale_mentioned_in_path?
        env['steam.locale_in_path']
      end

      def locale_not_mentioned_in_path?
        !locale_mentioned_in_path?
      end

      def path_with_locale
        modify_path do |segments|
          segments.insert(1, locale)
        end
      end

      def redirect_to_root_path_with_lang
        locale_not_mentioned_in_path? && path.gsub(/^\//, '') == '' && !default_locale?
      end
    end
  end

end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms_steam-1.8.0.alpha2 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.8.0.alpha1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.7.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/middlewares/locale_redirection.rb