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
- #default_locale ⇒ Object
- #default_locale_was ⇒ Object
-
#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.
- #is_default_locale?(locale) ⇒ Boolean
- #locale_fallbacks(locale) ⇒ Object
- #locales=(array) ⇒ Object
-
#localized? ⇒ Boolean
Tell if the site serves other locales than the default one.
-
#localized_page_fullpath(page, locale = nil) ⇒ String
TODO: use the Steam service instead.
- #prefix_default_locale? ⇒ Boolean
-
#with_default_locale(&block) ⇒ Object
Call yield within the Mongoid::Fields::I18 context of the default locale.
Instance Method Details
#default_locale ⇒ Object
65 66 67 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 65 def default_locale self.locales.first || Locomotive.config.site_locales.first end |
#default_locale_was ⇒ Object
73 74 75 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 73 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.
85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 85 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
69 70 71 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 69 def is_default_locale?(locale) locale.to_s == default_locale.to_s end |
#locale_fallbacks(locale) ⇒ Object
77 78 79 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 77 def locale_fallbacks(locale) [locale.to_s] + (locales - [locale.to_s]) end |
#locales=(array) ⇒ Object
61 62 63 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 61 def locales=(array) super((array || []).reject(&:blank?).map(&:to_s)) end |
#localized? ⇒ Boolean
Tell if the site serves other locales than the default one.
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'
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
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.
98 99 100 101 102 |
# File 'app/models/locomotive/concerns/site/locales.rb', line 98 def with_default_locale(&block) ::Mongoid::Fields::I18n.with_locale(self.default_locale) do yield end end |