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

Extended by:
ActiveSupport::Concern
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



67
68
69
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 67

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



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

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



51
52
53
54
55
56
57
58
59
60
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 51

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



36
37
38
39
40
41
42
# File 'app/models/locomotive/extensions/content_entry/localized.rb', line 36

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