Module: Locomotive::Concerns::Site::Locales

Extended by:
ActiveSupport::Concern
Included in:
Site
Defined in:
app/models/locomotive/concerns/site/locales.rb

Instance Method Summary collapse

Instance Method Details

#default_localeObject


39
40
41
# File 'app/models/locomotive/concerns/site/locales.rb', line 39

def default_locale
  self.locales.first || Locomotive.config.site_locales.first
end

#default_locale_wasObject


47
48
49
# File 'app/models/locomotive/concerns/site/locales.rb', line 47

def default_locale_was
  self.locales_was.try(:first) || Locomotive.config.site_locales.first
end

#each_locale(include_default_locale = true, &block) ⇒ Object

Iterate through all the locales of the site and for each of them call yield with the related Mongoid::Fields::I18n locale context. The first locale is the default one.


59
60
61
62
63
64
65
66
67
68
# File 'app/models/locomotive/concerns/site/locales.rb', line 59

def each_locale(include_default_locale = true, &block)
  current_locale = ::Mongoid::Fields::I18n.locale
  _locales = include_default_locale ? self.locales : (self.locales - [self.default_locale])

  _locales.each do |locale|
    ::Mongoid::Fields::I18n.with_locale(locale) do
      yield locale, current_locale.to_s == locale.to_s
    end
  end
end

#is_default_locale?(locale) ⇒ Boolean

Returns:


43
44
45
# File 'app/models/locomotive/concerns/site/locales.rb', line 43

def is_default_locale?(locale)
  locale.to_s == default_locale.to_s
end

#locale_fallbacks(locale) ⇒ Object


51
52
53
# File 'app/models/locomotive/concerns/site/locales.rb', line 51

def locale_fallbacks(locale)
  [locale.to_s] + (locales - [locale.to_s])
end

#locales=(array) ⇒ Object


35
36
37
# File 'app/models/locomotive/concerns/site/locales.rb', line 35

def locales=(array)
  super((array || []).reject(&:blank?).map(&:to_s))
end

#localized?Boolean

Tell if the site serves other locales than the default one.

Returns:

  • (Boolean)

    True if the number of locales is greater than 1


31
32
33
# File 'app/models/locomotive/concerns/site/locales.rb', line 31

def localized?
  self.locales.size > 1
end

#prefix_default_locale?Boolean

Returns:


23
24
25
# File 'app/models/locomotive/concerns/site/locales.rb', line 23

def prefix_default_locale?
  self.prefix_default_locale
end

#with_default_locale(&block) ⇒ Object

Call yield within the Mongoid::Fields::I18 context of the default locale.


72
73
74
75
76
# File 'app/models/locomotive/concerns/site/locales.rb', line 72

def with_default_locale(&block)
  ::Mongoid::Fields::I18n.with_locale(self.default_locale) do
    yield
  end
end