Module: Locomotive::Extensions::Site::Locales

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

Instance Method Summary collapse

Instance Method Details

#default_localeObject



64
65
66
# File 'app/models/locomotive/extensions/site/locales.rb', line 64

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

#default_locale_wasObject



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

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

#each_locale(&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.



84
85
86
87
88
89
90
# File 'app/models/locomotive/extensions/site/locales.rb', line 84

def each_locale(&block)
  self.locales.each do |locale|
    ::Mongoid::Fields::I18n.with_locale(locale) do
      yield locale
    end
  end
end

#is_default_locale?(locale) ⇒ Boolean

Returns:



68
69
70
# File 'app/models/locomotive/extensions/site/locales.rb', line 68

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

#locale_fallbacks(locale) ⇒ Object



76
77
78
# File 'app/models/locomotive/extensions/site/locales.rb', line 76

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

#locales=(array) ⇒ Object



60
61
62
# File 'app/models/locomotive/extensions/site/locales.rb', line 60

def locales=(array)
  array = [] if array.blank?; super(array)
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/extensions/site/locales.rb', line 31

def localized?
  self.locales.size > 1
end

#localized_page_fullpath(page, locale = nil) ⇒ String

Returns the fullpath of a page in the context of the current locale (I18n.locale) or the one passed in parameter. It also depends on the default site locale.

Ex:

For a site with its default site locale to 'en'
# context 1: i18n.locale is 'en'
contact_us.fullpath <= 'contact_us'

# context 2: i18n.locale is 'fr'
contact_us.fullpath <= 'fr/nous_contacter'

Parameters:

  • page (Page)

    The page we want the localized fullpath

  • locale (String) (defaults to: nil)

    The optional locale in place of the current one

Returns:

  • (String)

    The localized fullpath according to the current locale



51
52
53
54
55
56
57
58
# File 'app/models/locomotive/extensions/site/locales.rb', line 51

def localized_page_fullpath(page, locale = nil)
  return nil if page.fullpath_translations.blank?

  locale    = (locale || I18n.locale).to_s
  fullpath  = page.index? ? nil : (page.fullpath_translations[locale] || page.fullpath_translations[self.default_locale])
  locale_prefix = is_default_locale?(locale) && !prefix_default_locale ? nil : locale
  [locale_prefix, fullpath].compact.join '/'
end

#prefix_default_locale?Boolean

Returns:



23
24
25
# File 'app/models/locomotive/extensions/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.



94
95
96
97
98
# File 'app/models/locomotive/extensions/site/locales.rb', line 94

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