Module: Localizify

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/localizify.rb

Instance Method Summary collapse

Instance Method Details

#accept_localeObject



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_localeObject



57
58
59
# File 'app/controllers/concerns/localizify.rb', line 57

def cms_locale
  nil
end


69
70
71
# File 'app/controllers/concerns/localizify.rb', line 69

def cookie_locale
  cookies[:locale] && locale_available?(cookies[:locale]) ? cookies[:locale].try(:to_sym)  : nil
end

#cookify_localeObject



45
46
47
# File 'app/controllers/concerns/localizify.rb', line 45

def cookify_locale
  cookies[:locale] = { value: I18n.locale.to_s, expires: 1.year.from_now }
end

#default_locale?Boolean

Returns:

  • (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 default_url_options(options = {})
  options.merge(locale: I18n.locale || I18n.default_locale)
end

#domain_localeObject



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

Returns:

  • (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_localeObject



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_localeObject



41
42
43
# File 'app/controllers/concerns/localizify.rb', line 41

def parsed_locale
  cms_locale || param_locale || cookie_locale || accept_locale || I18n.default_locale
end

#set_available_localeObject



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_localeObject



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_localeObject



49
50
51
# File 'app/controllers/concerns/localizify.rb', line 49

def uncookify_locale
  cookies.delete(:locale)
end

#user_localeObject



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