Class: MissingTranslation::YamlProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/missing_translation/yaml_processor.rb

Class Method Summary collapse

Class Method Details

.denormalize_translations_hash(translations, parent_key = '') ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/missing_translation/yaml_processor.rb', line 4

def self.denormalize_translations_hash(translations, parent_key='')
  ans = {}
  translations.each do |key, val|
    if val.is_a? Hash
      aux = denormalize_translations_hash(val, "#{parent_key}#{key}.")
      aux.each do |k, v|
        ans[k] = v
      end
    else
      ans["#{parent_key}#{key}"] = val
    end
  end
  ans
end

.dig_set(obj, keys, value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/missing_translation/yaml_processor.rb', line 19

def self.dig_set(obj, keys, value)
  key = keys.first
  if keys.length == 1
    obj[key] = value
  else
    obj[key] = {} unless obj[key]
    dig_set(obj[key], keys.slice(1..-1), value)
  end
end

.normalize_translations_hash(hash) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/missing_translation/yaml_processor.rb', line 29

def self.normalize_translations_hash(hash)
  result = {}
  hash.keys.sort.each do |key|
    dig_set(result, key.split("."), hash[key])
  end
  result
end