Module: Locomotive::Extensions::ContentEntry::Localized

Included in:
ContentEntry
Defined in:
app/models/locomotive/extensions/content_entry/localized.rb

Instance Method Summary collapse

Instance Method Details

#localized?Boolean

Tell if the entry is localized or not, meaning if the label field is localized or not.

Returns:

  • (Boolean)

    True if localized, false otherwise



55
56
57
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 55

def localized?
  self.respond_to?(:"#{self._label_field_name}_translations")
end

#translated?Boolean

Tell if the content entry has been translated or not. It just checks if the field used for the label has been translated. It assumes the entry is localized.

Returns:

  • (Boolean)

    True if translated, false otherwise



12
13
14
15
16
17
18
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 12

def translated?
  if self.respond_to?(:"#{self._label_field_name}_translations")
    self.send(:"#{self._label_field_name}_translations").key?(::Mongoid::Fields::I18n.locale.to_s) #rescue false
  else
    true
  end
end

#translated_field?(field_or_name) ⇒ Boolean

Tell if the field of the content entry has been translated in the current locale or not.

Parameters:

  • field (String/Object)

    The field or the name of the field

Returns:

  • (Boolean)

    True if translated, false if not, nil if the field is not localized



39
40
41
42
43
44
45
46
47
48
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 39

def translated_field?(field_or_name)
  field = field_or_name.respond_to?(:name) ? field_or_name : self.fields[field_or_name.to_s]

  if field.try(:localized?)
    locale = ::Mongoid::Fields::I18n.locale.to_s
    (self.attributes[field.name] || {}).key?(locale)
  else
    nil
  end
end

#translated_inArray

Return the locales the content entry has been translated to.

Returns:

  • (Array)

    The list of locales. Nil if not localized



24
25
26
27
28
29
30
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 24

def translated_in
  if self.localized?
    self.send(:"#{self._label_field_name}_translations").keys
  else
    nil
  end
end