Class: Idioma::PhraseExtractor

Inherits:
Object
  • Object
show all
Defined in:
app/models/idioma/phrase_extractor.rb

Class Method Summary collapse

Class Method Details

.extractHash

Find all of the translations that are in the locale yaml files

and convert them into a hash of dotted hash notation key/values
for each locale that is found

Returns:

  • (Hash)

    Dotted hash notation key/value pairs



8
9
10
11
12
13
14
15
16
17
18
# File 'app/models/idioma/phrase_extractor.rb', line 8

def self.extract
  phrase_hashes = self.get_translations

  phrase_dotted_hashes = {}
  # convert each locales translations to dotted hash
  phrase_hashes.each do |locale, phrase_hash|
    phrase_dotted_hashes[locale] = self.to_dotted_hash(phrase_hash)
  end

  phrase_dotted_hashes
end

.get_translations(backend = nil) ⇒ Hash

Get translations from the i18n backend

Parameters:

  • A (I18n::Backend, nil)

    custom I18n backend

Returns:

  • (Hash)

    of translations separated by locale



23
24
25
26
27
# File 'app/models/idioma/phrase_extractor.rb', line 23

def self.get_translations(backend = nil)
  backend = I18n::Backend::Simple.new if backend.nil?
  backend.send(:init_translations)
  backend.send(:translations)
end

.to_dotted_hash(source, accumulator = {}, namespace = nil) ⇒ Hash

Convert a nested Hash ({format: {long: “YYYY-MM-DD”}})

to a dotted hash {"date.format.long" => "YYYY-MM-DD"}

Parameters:

  • Nested (Hash)

    hash input

  • An (Hash, {})

    accumulator to store the result

  • An (String, nil)

    accumulator for the key that gets built up

Returns:

  • (Hash)

    The end result after the conversion



35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/idioma/phrase_extractor.rb', line 35

def self.to_dotted_hash(source, accumulator = {}, namespace = nil)
  prefix = "#{namespace}." if namespace
  case source
  when Hash
    source.each do |key, value|
      to_dotted_hash(value, accumulator, "#{prefix}#{key}")
    end
  else
    accumulator[namespace] = source
  end
  accumulator
end