Module: RailsLocaleDetection::DetectionMethods
- Included in:
- LocaleDetector
- Defined in:
- lib/rails_locale_detection/detection_methods.rb
Instance Method Summary collapse
- #detect_locale ⇒ Object
- #locale_from(key) ⇒ Object
- #locale_from_cookie ⇒ Object
- #locale_from_param ⇒ Object
- #locale_from_request ⇒ Object
- #locale_from_user ⇒ Object
-
#set_default_url_option_for_request? ⇒ Boolean
returns true if the default url option should be set for this request.
-
#set_locale ⇒ Object
set I18n.locale, default_url_options and cookies to the value returned by detect_locale.
-
#validate_locale(locale) ⇒ Object
returns the (symbolized) value passed if it’s in the available_locales.
Instance Method Details
#detect_locale ⇒ Object
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 |
#locale_from_cookie ⇒ Object
12 13 14 |
# File 'lib/rails_locale_detection/detection_methods.rb', line 12 def validate_locale([locale_key]) end |
#locale_from_param ⇒ Object
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_request ⇒ Object
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_user ⇒ Object
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
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_locale ⇒ Object
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? [locale_key] = current_locale end if != current_locale [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 |