2012-01-26 01:33:39 +00:00
|
|
|
module Locomotive
|
|
|
|
module Extensions
|
|
|
|
module Site
|
|
|
|
module Locales
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
|
|
|
|
## fields ##
|
|
|
|
field :locales, :type => 'RawArray', :default => []
|
|
|
|
|
|
|
|
## callbacks ##
|
|
|
|
after_validation :add_default_locale
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
# 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'
|
|
|
|
#
|
|
|
|
# @params [ Page ] page The page we want the localized fullpath
|
|
|
|
# @params [ String ] locale The optional locale in place of the current one
|
|
|
|
#
|
|
|
|
# @returns [ String ] The localized fullpath according to the current locale
|
|
|
|
#
|
|
|
|
def localized_page_fullpath(page, locale = nil)
|
2012-03-01 10:07:22 +00:00
|
|
|
return nil if page.fullpath_translations.blank?
|
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
locale = (locale || I18n.locale).to_s
|
|
|
|
fullpath = page.fullpath_translations[locale] || page.fullpath_translations[self.default_locale]
|
|
|
|
|
|
|
|
locale == self.default_locale ? fullpath : File.join(locale, fullpath)
|
|
|
|
end
|
2012-01-26 01:33:39 +00:00
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
def locales=(array)
|
|
|
|
array = [] if array.blank?; super(array)
|
|
|
|
end
|
2012-01-26 01:33:39 +00:00
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
def default_locale
|
|
|
|
self.locales.first || Locomotive.config.site_locales.first
|
|
|
|
end
|
2012-01-26 01:33:39 +00:00
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
def locale_fallbacks(locale)
|
|
|
|
[locale.to_s] + (locales - [locale.to_s])
|
|
|
|
end
|
2012-01-26 01:33:39 +00:00
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
protected
|
2012-01-26 01:33:39 +00:00
|
|
|
|
2012-02-05 23:54:09 +00:00
|
|
|
def add_default_locale
|
|
|
|
self.locales = [Locomotive.config.site_locales.first] if self.locales.blank?
|
2012-01-26 01:33:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2012-02-05 23:54:09 +00:00
|
|
|
|
2012-01-26 01:33:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|