Module: I18nJS::EmbedFallbackTranslationsPlugin::Utils

Defined in:
lib/i18n-js/embed_fallback_translations_plugin.rb

Constant Summary collapse

PLURAL_KEYS =

Based on deep_merge by Stefan Rusterholz, see <www.ruby-forum.com/topic/142809>. This method is used to handle I18n fallbacks. Given two equivalent path nodes in two locale trees:

  1. If the node in the current locale appears to be an I18n pluralization (:one, :other, etc.), use the node, but merge in any missing/non-nil keys from the fallback (default) locale.

  2. Else if both nodes are Hashes, combine (merge) the key-value pairs of the two nodes into one, prioritizing the current locale.

  3. Else if either node is nil, use the other node.

%i[zero one two few many other].freeze
PLURAL_MERGER =
proc {|_key, v1, v2| v1 || v2 }
MERGER =
proc do |_key, v1, v2|
  if v1.is_a?(Hash) && v2.is_a?(Hash)
    if (v2.keys - PLURAL_KEYS).empty?
      v2.merge(v1, &PLURAL_MERGER).slice(*v2.keys)
    else
      v1.merge(v2, &MERGER)
    end
  else
    v2 || v1
  end
end

Class Method Summary collapse

Class Method Details

.deep_merge(target_hash, hash) ⇒ Object



33
34
35
# File 'lib/i18n-js/embed_fallback_translations_plugin.rb', line 33

def self.deep_merge(target_hash, hash)
  target_hash.merge(hash, &MERGER)
end