Module: EmbeddedLocalization::ActiveRecord::InstanceMethods

Defined in:
lib/embedded_localization/active_record/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#get_localized_attribute(attr_name, locale) ⇒ Object

Returns the translation of attr_name for the given locale; nil if there is none.

  • will convert given locale to symbol, e.g. "en","En" to :en
  • with fallbacks (see ClassMethods#fallbacks?), a nil translation is looked up in the fallback locales instead: the chain configured in I18n.fallbacks, then I18n.default_locale (see ClassMethods#fallback_locales)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 9

def get_localized_attribute(attr_name, locale)
  attr_name = attr_name.to_sym
  locale    = normalize_locale(locale)

  translation = translation_for(attr_name, locale)
  return translation unless translation.nil? && self.class.fallbacks?

  self.class.fallback_locales(locale).each do |fallback_locale|
    translation = translation_for(attr_name, fallback_locale)
    return translation unless translation.nil?
  end
  nil
end

#set_localized_attribute(attr_name, locale, new_translation) ⇒ Object

Sets the translation of attr_name for the given locale.

  • will convert given locale to symbol, e.g. "en","En" to :en
  • for I18n.default_locale, a DB column with the attribute's name (if the user defined one) is written too, so that the default locale values can be used in SQL queries
  • does nothing when the value is unchanged, so the record stays clean and its timestamps are not touched


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 28

def set_localized_attribute(attr_name, locale, new_translation)
  attr_name = attr_name.to_sym
  locale    = normalize_locale(locale)

  return if translation_unchanged?(attr_name, locale, new_translation)

  self.i18n_will_change!     # for ActiveModel Dirty tracking
  if native_column?(attr_name) && locale == I18n.default_locale
    write_attribute(attr_name, new_translation)
  end
  self.i18n ||= Hash.new
  self.i18n[locale] ||= Hash.new
  self.i18n[locale][attr_name] = new_translation
end

#translated?(name) ⇒ Boolean

Checks whether field with given name is translated field. Param String or Symbol Returns true or false

Returns:

  • (Boolean)


62
63
64
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 62

def translated?(name)
  self.class.translated?(name)
end

#translated_attributesObject

Returns Array of Symbols for all attributes of this class, which have translations through acts_as_i18n. returns an Array of Symbols



54
55
56
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 54

def translated_attributes
  self.class.translated_attributes
end

#translated_localesObject

Returns all locales used for translation of all documents of this class. returns an Array of Symbols



46
47
48
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 46

def translated_locales
  self.i18n.keys
end

#translation_coverage(attribute = nil) ⇒ Object

Purpose: to see the translation coverage Returns a Hash of all translated attributes, each with a Hash of the locales it has translations for



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 69

def translation_coverage( attribute = nil )
  attrs = {}
  self.i18n.each do |lang,hash|
    hash.keys.each do |attr|
      attrs[attr.to_sym] ||= []
      attrs[attr.to_sym] << lang
    end
  end
  if attribute.nil?
    return attrs
  elsif attrs[attribute.to_sym]
    attrs[attribute.to_sym]
  elsif translated?(attribute)
    []
  else   # if it's not a translated attribute, return nil
    nil
  end
end

#translation_missing(attribute = nil) ⇒ Object

Purpose: to quickly see if attribute translations are missing Returns a Hash of attributes, each with a Hash of the locales that are missing translations If an attribute has complete translation coverage, it will not be listed If the result is an empty Hash, then no attributes are missing translations

Needs all the desired locales to be present in 'translated_locales' e.g. each locale must be present in at least one of the translated attributes



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 96

def translation_missing( attribute = nil )
  missing = {}
  current_locales_used = translated_locales  # ... across all attributes

  translated_attributes.each do |attr|
    missing_locales = current_locales_used - translation_coverage(attr.to_sym)
    if missing_locales.size > 0
      missing[attr.to_sym] = missing_locales
    end
  end
  if attribute.nil?
    return missing
  else
    return missing[attribute.to_sym]
  end
end