Module: Locomotive::Concerns::ContentEntry::Localized

Extended by:
ActiveSupport::Concern
Included in:
Locomotive::ContentEntry
Defined in:
app/models/locomotive/concerns/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



68
69
70
# File 'app/models/locomotive/concerns/content_entry/localized.rb', line 68

def localized?
  self.respond_to?(:"#{_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



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

def translated?
  if self.respond_to?(:"#{_label_field_name}_translations")
    self.send(:"#{_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



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

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



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

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