Module: EmbeddedLocalization::ActiveRecord::InstanceMethods
- Defined in:
- lib/embedded_localization/active_record/instance_methods.rb
Instance Method Summary collapse
-
#get_localized_attribute(attr_name, locale) ⇒ Object
-
we only support fallbacks to I18n.default_locale for now - will convert given locale to symbol, e.g.
-
-
#set_localized_attribute(attr_name, locale, new_translation) ⇒ Object
-
will convert given locale to symbol, e.g.
-
-
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field.
-
#translated_attributes ⇒ Object
Returns Array of Symbols for all attributes of this class, which have translations through acts_as_i18n.
-
#translated_locales ⇒ Object
Returns all locales used for translation of all documents of this class.
-
#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.
-
#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.
Instance Method Details
#get_localized_attribute(attr_name, locale) ⇒ Object
-
we only support fallbacks to I18n.default_locale for now
-
will convert given locale to symbol, e.g. “en”,“En” to :en
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 8 def get_localized_attribute(attr_name, locale) locale = locale.downcase.to_sym if locale.class == String # ensure that locale is always a symbol if self.i18n.has_key?(locale) self.i18n[locale][attr_name] else if self.class.fallbacks? && self.i18n[ I18n.default_locale ] return self.i18n[ I18n.default_locale ][attr_name] else return nil end end end |
#set_localized_attribute(attr_name, locale, new_translation) ⇒ Object
-
will convert given locale to symbol, e.g. “en”,“En” to :en
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 24 def set_localized_attribute(attr_name, locale, new_translation) locale = locale.downcase.to_sym if locale.class == String # ensure that locale is always a symbol # first check if nothing changed - then we can just return, so that timestamps and other records don't get touched if self.i18n.class == Hash && (self.i18n[I18n.locale]) && (self.i18n[I18n.locale][attr_name.to_sym] == new_translation) return if (I18n.locale != I18n.default_locale) return if (I18n.locale == I18n.default_locale) && (read_attribute(attr_name) == new_translation) # both i18n and attr_name need to be equal to new_translation end self.i18n_will_change! # for ActiveModel Dirty tracking if self.attributes.has_key?(attr_name.to_s) # if user has defined DB field with that name write_attribute(attr_name , new_translation) if locale == I18n.default_locale end self.i18n[locale] ||= Hash.new self.i18n[locale][attr_name.to_sym] = new_translation end |
#translated?(name) ⇒ Boolean
Checks whether field with given name is translated field. Param String or Symbol Returns true or false
60 61 62 63 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 60 def translated?(name) self.class.translated?(name) # self.class.instance_variable_get(translated_attribute_names).include?(name.to_sym) end |
#translated_attributes ⇒ Object
Returns Array of Symbols for all attributes of this class,
which have translations through acts_as_i18n.
returns an Array of Symbols
52 53 54 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 52 def translated_attributes self.class.translated_attributes end |
#translated_locales ⇒ Object
Returns all locales used for translation of all documents of this class. returns an Array of Symbols
44 45 46 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 44 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 68 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 else return attrs[attribute.to_sym] 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
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/embedded_localization/active_record/instance_methods.rb', line 91 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 |