Sha256: 3f71cf7d79847097fbc48f2ddde7ecae09d0d6da73e15d356b1d6a5f38b49560

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 Bytes

Contents

module SimplificatorInfrastructure::LocaleDetection

  module InstanceMethods

    private

    def detect_locale
      locale_from_params || locale_from_header || locale_default
    end

    def locale_from_params
      locale = params['locale']
      available_locale_or_nil(locale)
    end

    def locale_from_header
      # Use "curl  -H 'Accept-language: fr' localhost:3000" to test it.

      header = request.env['HTTP_ACCEPT_LANGUAGE']
      return if header.blank?

      locale = header.scan(/^[a-z]{2}/).first
      available_locale_or_nil(locale)
    end

    def locale_default
      I18n.default_locale
    end

    def available_locale_or_nil(locale)
      I18n.available_locales.map(&:to_s).include?(locale) ? locale.to_sym : nil
    end
  end

  def self.included(receiver)
    receiver.send :include, InstanceMethods
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simplificator_infrastructure-0.1.1 lib/simplificator_infrastructure/locale_detection.rb
simplificator_infrastructure-0.1.0 lib/simplificator_infrastructure/locale_detection.rb
simplificator_infrastructure-0.0.11 lib/simplificator_infrastructure/locale_detection.rb
simplificator_infrastructure-0.0.10 lib/simplificator_infrastructure/locale_detection.rb