Module: Models::Translatable
- Extended by:
- ActiveSupport::Concern, Mobility
- Included in:
- CatalogItem, Item, ProductLine, ProductSpecification, Video
- Defined in:
- app/concerns/models/translatable.rb
Instance Attribute Summary collapse
-
#do_not_compact_translation_container ⇒ Object
Returns the value of attribute do_not_compact_translation_container.
Class Method Summary collapse
Instance Method Summary collapse
-
#compact_translation_container ⇒ Object
This method cleans up the translation container, prevents blanks from saving Also if a locale and its fallback are the same, only keep the fallback e.g if you have en-US and an en translation which are the same, we don't need to store and maintain en-US.
Instance Attribute Details
#do_not_compact_translation_container ⇒ Object
Returns the value of attribute do_not_compact_translation_container.
9 10 11 |
# File 'app/concerns/models/translatable.rb', line 9 def do_not_compact_translation_container @do_not_compact_translation_container end |
Class Method Details
.skip_compaction_for(*attributes) ⇒ Object
30 31 32 |
# File 'app/concerns/models/translatable.rb', line 30 def skip_compaction_for(*attributes) self.attributes_to_skip_compaction = attributes.map(&:to_s) end |
Instance Method Details
#compact_translation_container ⇒ Object
This method cleans up the translation container, prevents blanks from saving
Also if a locale and its fallback are the same, only keep the fallback
e.g if you have en-US and an en translation which are the same, we don't need
to store and maintain en-US
This works with column_fallback by moving values from sub-locales (en-US, en-CA)
to their base locale (en) and then removing the sub-locale
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/concerns/models/translatable.rb', line 44 def compact_translation_container return unless translations.present? translations.dup.each do |translation_locale, translated_values| locale_sym = translation_locale.to_sym _, locale_fallback = I18n.fallbacks[locale_sym] # Skip if there's no fallback or if this is already a base locale next unless locale_fallback.present? && locale_sym != locale_fallback # For sub-locales like en-US, en-CA that fall back to en: # Move their values to the fallback locale and remove the sub-locale translated_values.each do |key, value| # Skip if this attribute is marked to skip compaction next if attributes_to_skip_compaction.include?(key.to_s) backend_accessor = begin send(:"#{key}_backend") rescue StandardError nil end next if backend_accessor.nil? # Write the value to the fallback locale (this will use column_fallback if configured) backend_accessor.write(locale_fallback, value) # Clear the sub-locale value backend_accessor.write(locale_sym, nil) end # Remove the now-empty sub-locale from translations # But only if all keys were moved (i.e., none were skipped) all_keys_moved = translated_values.keys.all? { |k| attributes_to_skip_compaction.exclude?(k.to_s) } if all_keys_moved Rails.logger.info "Compacting translation locale: #{translation_locale} from #{self.class.name} #{id} (moved to fallback #{locale_fallback})" translations.delete(translation_locale) end end @do_not_compact_translation_container = true end |