Sha256: af17ea48f08a8a05fb28555aa3c1faaeea2b6a596173baf96bef92ac90f3e082

Contents?: true

Size: 951 Bytes

Versions: 22

Compression:

Stored size: 951 Bytes

Contents

module Locomotive::Wagon
  class Server

    # 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)
    #
    class Locale < Middleware

      def call(env)
        self.set_accessors(env)

        self.set_locale!(env)

        app.call(env)
      end

      protected

      def set_locale!(env)
        locale  = self.mounting_point.default_locale

        if self.path =~ /^(#{self.mounting_point.locales.join('|')})+(\/|$)/
          locale    = $1
          self.path = self.path.gsub($1 + $2, '')
          self.path = 'index' if self.path.blank?
        end

        Locomotive::Mounter.locale = locale
        ::I18n.locale = locale

        self.log "Detecting locale #{locale.upcase}"

        env['wagon.locale'] = locale
        env['wagon.path']   = self.path
      end

    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
locomotivecms_wagon-1.5.8 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.7 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.6 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.5 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.4 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.3 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.2 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.1 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.0 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.5.0.rc1 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.4.0 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.3.3 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.3.2 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.3.1 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.3.0 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.2.2 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.2.1 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.2.0 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.1.0 lib/locomotive/wagon/server/locale.rb
locomotivecms_wagon-1.0.2 lib/locomotive/wagon/server/locale.rb