Sha256: bc3b687939830888eb6ed53f68de589503fd1eb9416fe5f92a2a57fb93d0e4e5

Contents?: true

Size: 992 Bytes

Versions: 2

Compression:

Stored size: 992 Bytes

Contents

module Locomotive::Builder
  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

        puts "[Builder|Locale] path = #{self.path.inspect}, locale = #{locale.inspect}"

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

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms_builder-1.0.0.alpha2 lib/locomotive/builder/server/locale.rb
locomotivecms_builder-1.0.0.alpha1 lib/locomotive/builder/server/locale.rb