Module: Localizify
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/localizify.rb
Instance Method Summary collapse
- #accept_locale ⇒ Object
- #cms_locale ⇒ Object
- #cookie_locale ⇒ Object
- #cookify_locale ⇒ Object
- #default_locale? ⇒ Boolean
- #default_url_options(options = {}) ⇒ Object
- #domain_locale ⇒ Object
- #locale_available?(locale) ⇒ Boolean
- #param_locale ⇒ Object
- #parsed_locale ⇒ Object
- #set_available_locale ⇒ Object
- #set_locale ⇒ Object
- #uncookify_locale ⇒ Object
- #user_locale ⇒ Object
Instance Method Details
#accept_locale ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'app/controllers/concerns/localizify.rb', line 78 def accept_locale if request.env['HTTP_ACCEPT_LANGUAGE'].present? parsed_locale = request.env['HTTP_ACCEPT_LANGUAGE'].gsub(/\s+/, '').split(',').first parsed_locale = parsed_locale.scan(/^[a-z]{2}-[a-z]{2}|[a-z]{2}/i).first if parsed_locale parsed_locale = parsed_locale.split('-').last.try(:downcase).try(:to_sym) if parsed_locale locale_available?(parsed_locale) ? parsed_locale.try(:to_sym) : nil end end |
#cms_locale ⇒ Object
57 58 59 |
# File 'app/controllers/concerns/localizify.rb', line 57 def cms_locale nil end |
#cookie_locale ⇒ Object
69 70 71 |
# File 'app/controllers/concerns/localizify.rb', line 69 def [:locale] && locale_available?([:locale]) ? [:locale].try(:to_sym) : nil end |
#cookify_locale ⇒ Object
45 46 47 |
# File 'app/controllers/concerns/localizify.rb', line 45 def cookify_locale [:locale] = { value: I18n.locale.to_s, expires: 1.year.from_now } end |
#default_locale? ⇒ Boolean
10 11 12 |
# File 'app/controllers/concerns/localizify.rb', line 10 def default_locale? I18n.locale == I18n.default_locale end |
#default_url_options(options = {}) ⇒ Object
88 89 90 |
# File 'app/controllers/concerns/localizify.rb', line 88 def ( = {}) .merge(locale: I18n.locale || I18n.default_locale) end |
#domain_locale ⇒ Object
73 74 75 76 |
# File 'app/controllers/concerns/localizify.rb', line 73 def domain_locale parsed_locale = request.host.split('.').last.try(:to_s) locale_available?(parsed_locale) ? parsed_locale.try(:to_sym) : nil end |
#locale_available?(locale) ⇒ Boolean
53 54 55 |
# File 'app/controllers/concerns/localizify.rb', line 53 def locale_available?(locale) I18n.available_locales.include?(locale.try(:to_sym)) end |
#param_locale ⇒ Object
65 66 67 |
# File 'app/controllers/concerns/localizify.rb', line 65 def param_locale locale_available?(params[:locale]) ? params[:locale].try(:to_sym) : nil end |
#parsed_locale ⇒ Object
41 42 43 |
# File 'app/controllers/concerns/localizify.rb', line 41 def parsed_locale cms_locale || param_locale || || accept_locale || I18n.default_locale end |
#set_available_locale ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'app/controllers/concerns/localizify.rb', line 14 def set_available_locale begin unless (cms_locales = ::Comfy::Cms::Site.pluck(:locale)).any? cms_locales = [I18n.default_locale] end rescue cms_locales = [I18n.default_locale] end I18n.available_locales = cms_locales.uniq end |
#set_locale ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'app/controllers/concerns/localizify.rb', line 26 def set_locale return true if kind_of?(Comfy::Cms::ContentController) unless [params[:locale].try(:to_sym)].compact.include?(parsed_locale.to_sym) begin redirect_to url_for(locale: parsed_locale) rescue redirect_to root_path(locale: parsed_locale) end end I18n.locale = parsed_locale cookify_locale end |
#uncookify_locale ⇒ Object
49 50 51 |
# File 'app/controllers/concerns/localizify.rb', line 49 def uncookify_locale .delete(:locale) end |
#user_locale ⇒ Object
61 62 63 |
# File 'app/controllers/concerns/localizify.rb', line 61 def user_locale current_user && locale_available?(current_user.domain) ? current_user.domain.to_sym : nil end |