Module: RailsLocaleDetection::DetectionMethods

Included in:
LocaleDetector
Defined in:
lib/rails_locale_detection/detection_methods.rb

Instance Method Summary collapse

Instance Method Details

#detect_localeObject



28
29
30
# 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) ⇒ Object



24
25
26
# File 'lib/rails_locale_detection/detection_methods.rb', line 24

def locale_from(key)
  send("locale_from_#{key}")
end


12
13
14
# File 'lib/rails_locale_detection/detection_methods.rb', line 12

def locale_from_cookie
  validate_locale(cookies[locale_key])
end

#locale_from_paramObject



8
9
10
# File 'lib/rails_locale_detection/detection_methods.rb', line 8

def locale_from_param
  validate_locale(params[locale_key])
end

#locale_from_requestObject



16
17
18
# 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_userObject



20
21
22
# 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?Boolean

returns true if the default url option should be set for this request

Returns:

  • (Boolean)


50
51
52
# 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_localeObject

set I18n.locale, default_url_options and cookies to the value returned by detect_locale



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object

returns the (symbolized) value passed if it’s in the available_locales



4
5
6
# 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