Module: I18n::JS::Utils

Defined in:
lib/i18n/js/utils.rb

Constant Summary collapse

PLURAL_KEYS =
%i[zero one two few many other].freeze
PLURAL_MERGER =

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.

proc do |_key, v1, v2|
  v1 || v2
end
MERGER =
proc do |_key, v1, v2|
  if Hash === v1 && Hash === v2
    if (v2.keys - PLURAL_KEYS).empty?
      slice(v2.merge(v1, &PLURAL_MERGER), v2.keys)
    else
      v1.merge(v2, &MERGER)
    end
  else
    v2 || v1
  end
end
HASH_NIL_VALUE_CLEANER_PROC =
proc do |k, v|
  v.kind_of?(Hash) ? (v.delete_if(&HASH_NIL_VALUE_CLEANER_PROC); false) : v.nil?
end

Class Method Summary collapse

Class Method Details

.deep_key_sort(hash) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/i18n/js/utils.rb', line 68

def self.deep_key_sort(hash)
  # Avoid things like `true` or `1` from YAML which causes error
  hash.keys.sort {|a, b| a.to_s <=> b.to_s}.
    each_with_object({}) do |key, seed|
    value = hash[key]
    seed[key] = value.is_a?(Hash) ? deep_key_sort(value) : value
  end
end

.deep_merge(target_hash, hash) ⇒ Object

:nodoc:



44
45
46
# File 'lib/i18n/js/utils.rb', line 44

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

.deep_merge!(target_hash, hash) ⇒ Object

:nodoc:



48
49
50
# File 'lib/i18n/js/utils.rb', line 48

def self.deep_merge!(target_hash, hash) # :nodoc:
  target_hash.merge!(hash, &MERGER)
end

.deep_reject(hash, scopes = [], &block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/i18n/js/utils.rb', line 52

def self.deep_reject(hash, scopes = [], &block)
  hash.each_with_object({}) do |(k, v), memo|
    unless block.call(k, v, scopes + [k.to_s])
      memo[k] = v.kind_of?(Hash) ? deep_reject(v, scopes + [k.to_s], &block) : v
    end
  end
end

.deep_remove_procs(hash) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/i18n/js/utils.rb', line 77

def self.deep_remove_procs(hash)
  # procs exist in `i18n.plural.rule` as pluralizer
  # But having it in translation causes the exported JS/JSON changes every time
  # https://github.com/ruby-i18n/i18n/blob/v1.8.7/lib/i18n/backend/pluralization.rb#L51
  hash.keys.
    each_with_object({}) do |key, seed|
    value = hash[key]
    next if value.is_a?(Proc)

    seed[key] = value.is_a?(Hash) ? deep_remove_procs(value) : value
  end
end

.scopes_match?(scopes1, scopes2) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/i18n/js/utils.rb', line 60

def self.scopes_match?(scopes1, scopes2)
  if scopes1.length == scopes2.length
    [scopes1, scopes2].transpose.all? do |scope1, scope2|
      scope1.to_s == '*' || scope2.to_s == '*' || scope1.to_s == scope2.to_s
    end
  end
end

.slice(hash, keys) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/i18n/js/utils.rb', line 32

def self.slice(hash, keys)
  if hash.respond_to?(:slice) # ruby 2.5 onwards
    hash.slice(*keys)
  else
    hash.select {|key, _| keys.include?(key)}
  end
end

.strip_keys_with_nil_values(hash) ⇒ Object



40
41
42
# File 'lib/i18n/js/utils.rb', line 40

def self.strip_keys_with_nil_values(hash)
  hash.dup.delete_if(&HASH_NIL_VALUE_CLEANER_PROC)
end