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



66
67
68
# File 'app/models/locomotive/concerns/site/locales.rb', line 66

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

#default_locale_wasObject



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

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.



86
87
88
89
90
91
92
93
94
95
# File 'app/models/locomotive/concerns/site/locales.rb', line 86

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:



70
71
72
# File 'app/models/locomotive/concerns/site/locales.rb', line 70

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

#locale_fallbacks(locale) ⇒ Object



78
79
80
# File 'app/models/locomotive/concerns/site/locales.rb', line 78

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

#locales=(array) ⇒ Object



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

def locales=(array)
  array.reject!(&:blank?)
  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



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

def localized?
  self.locales.size > 1
end

#localized_page_fullpath(page, locale = nil) ⇒ String

TODO: use the Steam service instead

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



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

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:



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

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.



99
100
101
102
103
# File 'app/models/locomotive/concerns/site/locales.rb', line 99

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