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



62
63
64
# File 'lib/spree/core/controller_helpers/locale.rb', line 62

def available_locales
  Spree::Store.available_locales
end

#config_locale?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/spree/core/controller_helpers/locale.rb', line 36

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

#current_localeObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spree/core/controller_helpers/locale.rb', line 24

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



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

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



66
67
68
69
70
# File 'lib/spree/core/controller_helpers/locale.rb', line 66

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)


40
41
42
# File 'lib/spree/core/controller_helpers/locale.rb', line 40

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

#set_localeObject



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

def set_locale
  I18n.locale = current_locale
end

#supported_locale?(locale_code) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/spree/core/controller_helpers/locale.rb', line 52

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

  supported_locales.include?(locale_code&.to_s)
end

#supported_localesObject



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

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

#supported_locales_for_all_storesObject



58
59
60
# File 'lib/spree/core/controller_helpers/locale.rb', line 58

def supported_locales_for_all_stores
  @supported_locales_for_all_stores ||= Spree.available_locales
end

#user_locale?Boolean

Returns:

  • (Boolean)


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

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