Class: I18nTemplate::Extractor::Yaml

Inherits:
Base
  • Object
show all
Defined in:
lib/i18n_template/extractor/yaml.rb

Overview

Extract phrases to yaml format. E.g: config/locales/templates.yml

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from I18nTemplate::Extractor::Base

Class Method Details

.default_optionsObject


16
17
18
19
20
# File 'lib/i18n_template/extractor/yaml.rb', line 16

def default_options
  super.merge({
    :locales_root => 'config/locales'
  })
end

.formatObject


12
13
14
# File 'lib/i18n_template/extractor/yaml.rb', line 12

def format
  'yaml'
end

Instance Method Details

#call(paths) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/i18n_template/extractor/yaml.rb', line 23

def call(paths)
  # ensure root directory exists
  FileUtils.mkdir_p(@options[:locales_root])
  output_file = File.join(@options[:locales_root], 'phrases.yml')

  # extract phrases
  phrases = []
  paths.each do |path|
    phrases += extract_phrases(path)
  end
  phrases.uniq!

  # update phrases
  log "Extracting #{phrases.size} phrases to #{output_file}"
  data = File.exists?(output_file) ? YAML.load_file(output_file) : {}
  data['en'] ||= {}
  data.keys.each do |locale|
    phrases.each do |phrase|
      data[locale][phrase] ||= nil
    end
  end

  # store data
  File.open(output_file, "w") do |f|
    f << data.to_yaml
  end
end