module LocaleDetect::Filter

Protected Instance Methods

accept_locale() click to toggle source
# File lib/locale_detect/filter.rb, line 37
def accept_locale
  logger.debug "* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}"
  logger.debug "* available_locales: #{::Rails.application.config.available_locales}"
  lang = header_lang
  logger.debug "* header_langs: #{lang}"
  lang.first unless lang.empty?
end
detect_locale() click to toggle source
# File lib/locale_detect/filter.rb, line 20
def detect_locale
  if params[:locale].present?
    locale = lang_to_locale(params[:locale])
    if locale.nil?
      raise LocaleDetect::BadLocale.new(params[:locale])
    end
    I18n.locale = session[:locale] = locale
  else
    I18n.locale = session_locale || accept_locale || I18n.default_locale.to_s
  end        
  logger.debug "* Locale set to '#{I18n.locale}'"
end
header_lang() click to toggle source
# File lib/locale_detect/filter.rb, line 45
def header_lang
  accept_langs = request.env['HTTP_ACCEPT_LANGUAGE']
  return if accept_langs.nil?

  lang_and_q = accept_langs.split(",").map { |l|
    l += ';q=1.0' unless l =~ /;q=\d+(?:\.\d+)?$/
    l.split(';q=')
  }
  lang = lang_and_q.sort_by { |(l, q)|
    -q.to_f
  }
  lang = lang.map { |(l, q)| lang_to_locale(l) }
  lang.compact
end
lang_to_locale(l) click to toggle source
# File lib/locale_detect/filter.rb, line 33
def lang_to_locale(l)
  return l if ::Rails.application.config.available_locales.include? l
end
session_locale() click to toggle source
# File lib/locale_detect/filter.rb, line 60
def session_locale
  logger.debug "* Session: #{session[:locale]}"
  lang_to_locale(session[:locale]) if session[:locale].present?
end