Module: Stay::LocaleHelper
- Included in:
- ApplicationController
- Defined in:
- app/helpers/stay/locale_helper.rb
Instance Method Summary collapse
- #all_locales_options ⇒ Object
- #available_locales ⇒ Object
- #available_locales_options ⇒ Object
- #config_locale? ⇒ Boolean
-
#current_locale ⇒ Object
New methods added from the controller.
-
#find_with_fallback_default_locale(&block) ⇒ Object
Optionally, you can keep this as a helper method for views.
- #locale_full_name(locale) ⇒ Object
- #locale_param ⇒ Object
- #locale_presentation(locale) ⇒ Object
- #params_locale? ⇒ Boolean
- #should_render_locale_dropdown? ⇒ Boolean
- #supported_locale?(locale_code) ⇒ Boolean
- #supported_locales ⇒ Object
- #supported_locales_for_all_stores ⇒ Object
- #supported_locales_options ⇒ Object
- #user_locale? ⇒ Boolean
Instance Method Details
#all_locales_options ⇒ Object
3 4 5 |
# File 'app/helpers/stay/locale_helper.rb', line 3 def supported_locales_for_all_stores.map { |locale| locale_presentation(locale) } end |
#available_locales ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/helpers/stay/locale_helper.rb', line 21 def available_locales locales_from_i18n = I18n.available_locales locales = if defined?(StayI18n) (StayI18n::Locale.all << :en).map(&:to_sym) else [Rails.application.config.i18n.default_locale, I18n.locale, :en] end (locales + locales_from_i18n).uniq.compact end |
#available_locales_options ⇒ Object
7 8 9 |
# File 'app/helpers/stay/locale_helper.rb', line 7 def available_locales.map { |locale| locale_presentation(locale) } end |
#config_locale? ⇒ Boolean
86 87 88 |
# File 'app/helpers/stay/locale_helper.rb', line 86 def config_locale? respond_to?(:config_locale, true) && config_locale.present? end |
#current_locale ⇒ Object
New methods added from the controller
56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/helpers/stay/locale_helper.rb', line 56 def current_locale @current_locale ||= if user_locale? 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
Optionally, you can keep this as a helper method for views
97 98 99 100 101 102 103 104 105 |
# File 'app/helpers/stay/locale_helper.rb', line 97 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_full_name(locale) ⇒ Object
45 46 47 |
# File 'app/helpers/stay/locale_helper.rb', line 45 def locale_full_name(locale) I18n.t('i18n.this_file_language', locale: locale) end |
#locale_param ⇒ Object
90 91 92 93 94 |
# File 'app/helpers/stay/locale_helper.rb', line 90 def locale_param return if I18n.locale.to_s == current_store.default_locale || current_store.default_locale.nil? I18n.locale.to_s end |
#locale_presentation(locale) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/helpers/stay/locale_helper.rb', line 33 def locale_presentation(locale) if I18n.exists?('stay.i18n.this_file_language', locale: locale, fallback: false) [locale_full_name(locale), locale.to_s] elsif defined?(StayI18n::Locale) && (language_name = StayI18n::Locale.local_language_name(locale)) ["#{language_name} (#{locale})", locale.to_s] elsif locale.to_s == 'en' ['English (US)', 'en'] else [locale, locale.to_s] end end |
#params_locale? ⇒ Boolean
78 79 80 |
# File 'app/helpers/stay/locale_helper.rb', line 78 def params_locale? params[:locale].present? && supported_locale?(params[:locale]) end |
#should_render_locale_dropdown? ⇒ Boolean
49 50 51 52 53 |
# File 'app/helpers/stay/locale_helper.rb', line 49 def should_render_locale_dropdown? return false if current_store.nil? current_store.supported_locales_list.size > 1 end |
#supported_locale?(locale_code) ⇒ Boolean
72 73 74 75 76 |
# File 'app/helpers/stay/locale_helper.rb', line 72 def supported_locale?(locale_code) return false if supported_locales.nil? supported_locales.include?(locale_code&.to_s) end |
#supported_locales ⇒ Object
68 69 70 |
# File 'app/helpers/stay/locale_helper.rb', line 68 def supported_locales @supported_locales ||= current_store&.supported_locales_list end |
#supported_locales_for_all_stores ⇒ Object
17 18 19 |
# File 'app/helpers/stay/locale_helper.rb', line 17 def supported_locales_for_all_stores @supported_locales_for_all_stores ||= available_locales end |
#supported_locales_options ⇒ Object
11 12 13 14 15 |
# File 'app/helpers/stay/locale_helper.rb', line 11 def return if current_store.nil? current_store.supported_locales_list.map { |locale| locale_presentation(locale) } end |
#user_locale? ⇒ Boolean
82 83 84 |
# File 'app/helpers/stay/locale_helper.rb', line 82 def user_locale? Stay::Config.use_user_locale && current_user && supported_locale?(current_user.selected_locale) end |