Module: Spree::Core::ControllerHelpers::Locale
- Extended by:
- ActiveSupport::Concern
- Included in:
- BaseController
- Defined in:
- lib/spree/core/controller_helpers/locale.rb
Instance Method Summary collapse
- #available_locales ⇒ Object
- #config_locale? ⇒ Boolean
- #current_locale ⇒ Object
- #default_locale ⇒ Object
- #find_with_fallback_default_locale(&block) ⇒ Object
- #locale_param ⇒ Object
- #params_locale? ⇒ Boolean
- #set_fallback_locale ⇒ Object
- #set_locale ⇒ Object
- #supported_locale?(locale_code) ⇒ Boolean
- #supported_locales ⇒ Object
- #supported_locales_for_all_stores ⇒ Object
- #user_locale? ⇒ Boolean
Instance Method Details
#available_locales ⇒ Object
77 78 79 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 77 def available_locales Spree::Store.available_locales end |
#config_locale? ⇒ Boolean
51 52 53 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 51 def config_locale? respond_to?(:config_locale, true) && config_locale.present? end |
#current_locale ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 39 def current_locale @current_locale ||= if user_locale? try_spree_current_user.selected_locale elsif params_locale? params[:locale] elsif config_locale? config_locale else default_locale end end |
#default_locale ⇒ Object
35 36 37 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 35 def default_locale @default_locale ||= current_store&.default_locale || Rails.application.config.i18n.default_locale || I18n.default_locale end |
#find_with_fallback_default_locale(&block) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 87 def find_with_fallback_default_locale(&block) result = begin block.call rescue ActiveRecord::RecordNotFound => _e nil end result || Mobility.with_locale(current_store.default_locale) { block.call } end |
#locale_param ⇒ Object
81 82 83 84 85 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 81 def locale_param return if I18n.locale.to_s == current_store.default_locale || current_store.default_locale.nil? I18n.locale.to_s end |
#params_locale? ⇒ Boolean
55 56 57 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 55 def params_locale? params[:locale].present? && supported_locale?(params[:locale]) end |
#set_fallback_locale ⇒ Object
29 30 31 32 33 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 29 def set_fallback_locale return unless respond_to?(:current_store) && current_store.present? Spree::Locales::SetFallbackLocaleForStore.new.call(store: current_store) end |
#set_locale ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 21 def set_locale # Request-local content locale — the fallback for storeless hosts # lives in the Spree::Current#content_locale reader; never write the # process-global I18n.default_locale, which all threads share. Spree::Current.content_locale = current_store&.default_locale I18n.locale = current_locale end |
#supported_locale?(locale_code) ⇒ Boolean
67 68 69 70 71 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 67 def supported_locale?(locale_code) return false if supported_locales.nil? supported_locales.include?(locale_code&.to_s) end |
#supported_locales ⇒ Object
63 64 65 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 63 def supported_locales @supported_locales ||= current_store&.supported_locales_list end |
#supported_locales_for_all_stores ⇒ Object
73 74 75 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 73 def supported_locales_for_all_stores @supported_locales_for_all_stores ||= Spree.available_locales end |
#user_locale? ⇒ Boolean
59 60 61 |
# File 'lib/spree/core/controller_helpers/locale.rb', line 59 def user_locale? Spree::Config.use_user_locale && try_spree_current_user && supported_locale?(try_spree_current_user.selected_locale) end |