Sha256: a31bd65e3b2f336c141af1e6ab6ff43530249cfb5128298241179aa08d9b0478

Contents?: true

Size: 1.37 KB

Versions: 17

Compression:

Stored size: 1.37 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 Helpers

      def _call
        if url = redirect_url
          redirect_to url
        end
      end

      protected

      def redirect_url
        if apply_redirection?
          if site.prefix_default_locale
            path_with_default_locale if locale_not_mentioned_in_path?
          else
            path_without_default_locale if default_locale? && locale_mentioned_in_path?
          end
        end
      end

      def apply_redirection?
        site.locales.size > 1 && request.get?
      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_default_locale
        modify_path do |segments|
          segments.insert(1, site.default_locale)
        end
      end

      def path_without_default_locale
        modify_path do |segments|
          segments.delete_at(1)
        end
      end

    end
  end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc10 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc9 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc8 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc6 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc4 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc3 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc2 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.rc1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.beta.3 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.beta.2 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.beta.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.alpha.3 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.alpha.2 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.alpha.1 lib/locomotive/steam/middlewares/locale_redirection.rb
locomotivecms_steam-1.0.0.pre.alpha lib/locomotive/steam/middlewares/locale_redirection.rb