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

Instance Method Details

#available_localesObject



69
70
71
# File 'lib/spree/core/controller_helpers/locale.rb', line 69

def available_locales
  Spree::Store.available_locales
end

#config_locale?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/spree/core/controller_helpers/locale.rb', line 43

def config_locale?
  respond_to?(:config_locale, true) && config_locale.present?
end

#current_localeObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spree/core/controller_helpers/locale.rb', line 31

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
                        current_store&.default_locale || Rails.application.config.i18n.default_locale || I18n.default_locale
                      end
end

#find_with_fallback_default_locale(&block) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/spree/core/controller_helpers/locale.rb', line 79

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_paramObject



73
74
75
76
77
# File 'lib/spree/core/controller_helpers/locale.rb', line 73

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

Returns:

  • (Boolean)


47
48
49
# File 'lib/spree/core/controller_helpers/locale.rb', line 47

def params_locale?
  params[:locale].present? && supported_locale?(params[:locale])
end

#set_fallback_localeObject



25
26
27
28
29
# File 'lib/spree/core/controller_helpers/locale.rb', line 25

def set_fallback_locale
  return unless respond_to?(:current_store) && current_store.present?

  Spree::Locales::SetFallbackLocaleForStore.new.call(store: current_store)
end

#set_localeObject



21
22
23
# File 'lib/spree/core/controller_helpers/locale.rb', line 21

def set_locale
  I18n.locale = current_locale
end

#supported_locale?(locale_code) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/spree/core/controller_helpers/locale.rb', line 59

def supported_locale?(locale_code)
  return false if supported_locales.nil?

  supported_locales.include?(locale_code&.to_s)
end

#supported_localesObject



55
56
57
# File 'lib/spree/core/controller_helpers/locale.rb', line 55

def supported_locales
  @supported_locales ||= current_store&.supported_locales_list
end

#supported_locales_for_all_storesObject



65
66
67
# File 'lib/spree/core/controller_helpers/locale.rb', line 65

def supported_locales_for_all_stores
  @supported_locales_for_all_stores ||= Spree.available_locales
end

#user_locale?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/spree/core/controller_helpers/locale.rb', line 51

def user_locale?
  Spree::Config.use_user_locale && try_spree_current_user && supported_locale?(try_spree_current_user.selected_locale)
end