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



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

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

#default_locale_wasObject



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

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.



81
82
83
84
85
86
87
# File 'app/models/locomotive/extensions/site/locales.rb', line 81

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

#locale_fallbacks(locale) ⇒ Object



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

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

#locales=(array) ⇒ Object



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

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



26
27
28
# File 'app/models/locomotive/extensions/site/locales.rb', line 26

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/locomotive/extensions/site/locales.rb', line 46

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

  locale    = (locale || I18n.locale).to_s
  fullpath  = page.fullpath_translations[locale] || page.fullpath_translations[self.default_locale]

  if locale == self.default_locale.to_s # no need to specify the locale
    page.index? ? '' : fullpath
  elsif page.index? # avoid /en/index or /fr/index, prefer /en or /fr instead
    locale
  else
    File.join(locale, fullpath)
  end
end

#with_default_locale(&block) ⇒ Object

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



91
92
93
94
95
# File 'app/models/locomotive/extensions/site/locales.rb', line 91

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