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



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

def available_locales
  Spree::Store.available_locales
end

#config_locale?Boolean

Returns:

  • (Boolean)


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

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

#current_localeObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spree/core/controller_helpers/locale.rb', line 36

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_localeObject



32
33
34
# File 'lib/spree/core/controller_helpers/locale.rb', line 32

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



84
85
86
87
88
89
90
91
92
# File 'lib/spree/core/controller_helpers/locale.rb', line 84

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



78
79
80
81
82
# File 'lib/spree/core/controller_helpers/locale.rb', line 78

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)


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

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

#set_fallback_localeObject



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

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
24
# File 'lib/spree/core/controller_helpers/locale.rb', line 21

def set_locale
  I18n.default_locale = default_locale unless Spree.always_use_translations?
  I18n.locale = current_locale
end

#supported_locale?(locale_code) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  supported_locales.include?(locale_code&.to_s)
end

#supported_localesObject



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

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

#supported_locales_for_all_storesObject



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

def supported_locales_for_all_stores
  @supported_locales_for_all_stores ||= Spree.available_locales
end

#user_locale?Boolean

Returns:

  • (Boolean)


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

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