Sha256: cc07e76a5626385aecc22543779bac50dfa686d044b860eb2fb956c5e3a5e988

Contents?: true

Size: 1.14 KB

Versions: 10

Compression:

Stored size: 1.14 KB

Contents

module Locomotive::Steam
  module Middlewares

    # Set the locale from the path if possible or use the default one
    #
    # Examples:
    #
    #   /fr/index   => locale = :fr
    #   /fr/        => locale = :fr
    #   /index      => locale = :en (default one)
    #
    # The
    #
    class Locale < ThreadSafe

      include Helpers

      def _call
        locale = extract_locale

        log "Detecting locale #{locale.upcase}"

        I18n.with_locale(locale) do
          self.next
        end
      end

      protected

      def extract_locale
        _locale = locale_from_params || default_locale
        _path   = request.path_info

        if _path =~ /^\/(#{site.locales.join('|')})+(\/|$)/
          _locale  = $1
          _path    = _path.gsub($1 + $2, '')

          # let the other middlewares that the locale was
          # extracted from the path.
          env['steam.locale_in_path'] = true
        end

        env['steam.path']   = _path
        env['steam.locale'] = services.locale = _locale
      end

      def locale_from_params
        locales.include?(params[:locale]) ? params[:locale] : nil
      end

    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
locomotivecms_steam-1.4.1 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.4.0 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.4.0.rc2 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.4.0.rc1 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.4.0.pre.rc.1 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.3.0 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.3.0.rc2 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.1.2 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.2.1 lib/locomotive/steam/middlewares/locale.rb
locomotivecms_steam-1.3.0.rc1 lib/locomotive/steam/middlewares/locale.rb