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.first || Locomotive.config.site_locales.first
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) ⇒ Object

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'


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