module RailsLocaleDetection::DetectionMethods
Public Instance Methods
detect_locale()
click to toggle source
# File lib/rails_locale_detection/detection_methods.rb, line 28 def detect_locale RailsLocaleDetection.detection_order.inject(nil) { |result, source| result || locale_from(source) } || default_locale end
locale_from(key)
click to toggle source
# File lib/rails_locale_detection/detection_methods.rb, line 24 def locale_from(key) send("locale_from_#{key}") end
locale_from_param()
click to toggle source
# File lib/rails_locale_detection/detection_methods.rb, line 8 def locale_from_param validate_locale(params[locale_key]) end
locale_from_request()
click to toggle source
# File lib/rails_locale_detection/detection_methods.rb, line 16 def locale_from_request validate_locale(http_accept_language.preferred_language_from(available_locales)) end
locale_from_user()
click to toggle source
# File lib/rails_locale_detection/detection_methods.rb, line 20 def locale_from_user validate_locale(user_locale) end
set_default_url_option_for_request?()
click to toggle source
returns true if the default url option should be set for this request
# File lib/rails_locale_detection/detection_methods.rb, line 50 def set_default_url_option_for_request? RailsLocaleDetection.set_default_url_option === true || RailsLocaleDetection.set_default_url_option == :always || RailsLocaleDetection.set_default_url_option == :explicitly && params[locale_key].present? end
set_locale()
click to toggle source
set I18n.locale, default_url_options and cookies to the value returned by detect_locale
# File lib/rails_locale_detection/detection_methods.rb, line 34 def set_locale self.current_locale = detect_locale if set_default_url_option_for_request? default_url_options[locale_key] = current_locale end if locale_from_cookie != current_locale cookies[locale_key] = { :value => current_locale, :expires => RailsLocaleDetection.locale_expiry.from_now } end end
validate_locale(locale)
click to toggle source
returns the (symbolized) value passed if it's in the available_locales
# File lib/rails_locale_detection/detection_methods.rb, line 4 def validate_locale(locale) locale.to_sym if locale && available_locales.include?(locale.to_sym) end