Class: ZLocalize::Harvester

Inherits:
Object
  • Object
show all
Defined in:
lib/zlocalize/harvester.rb

Overview

Harvester will parse and extract all calls to translation methods in “app/models/”, “app/controllers/” and “app/views/”

Constant Summary collapse

DEFAULT_HARVEST_OPTIONS =
{ :output => 'config/translations/app-strings.yml',
:overwrite_existing => false,
:add_paths   => [],
:silent => false,
:purge => false }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rails_root, options = {}) ⇒ Harvester

Returns a new instance of Harvester.



24
25
26
27
# File 'lib/zlocalize/harvester.rb', line 24

def initialize(rails_root, options = {})
  @rails_root = rails_root
  @options = { paths: ZLocalize.config.harvest_paths }.merge(DEFAULT_HARVEST_OPTIONS).merge(options).with_indifferent_access
end

Instance Attribute Details

#rails_rootObject

Returns the value of attribute rails_root.



16
17
18
# File 'lib/zlocalize/harvester.rb', line 16

def rails_root
  @rails_root
end

Instance Method Details

#collect_entries(filename, root, is_erb = false) ⇒ Object

collect entries in a source file



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/zlocalize/harvester.rb', line 85

def collect_entries(filename,root,is_erb = false)
  begin
    sp = ZLocalize::SourceProcessor.new(filename,root,is_erb)
  rescue Exception => e
    puts "Error occured while parsing #{filename}:\n\n#{e.message}"
    return
  end
  sp.translation_entries.each do |key,te|
    if @new_entries[key]
      te.references.each do |ref|
        @new_entries[key].add_reference(ref)
      end
    else
      @new_entries[key] = te.dup
    end
  end
end

#harvestObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/zlocalize/harvester.rb', line 46

def harvest
  @new_entries = TranslationEntryCollection.new
  @options[:paths].each do |path|
    harvest_path(path)
    progress("\n")
  end

  @translation_file = ZLocalize::TranslationFile.new
  unless @options[:overwrite_existing]
    progress("Merging existing translations from #{@options[:output]}...\n")
    begin
      @translation_file.load(File.join(@rails_root,@options[:output]))
    rescue
    end
  end

  @translation_file.entries.synchronize_with(@new_entries,@options[:purge])

  progress("Writing new translation file...\n")
  File.open(File.join(@rails_root,@options[:output]),"w") do |f|
    f.write(@translation_file.to_yaml)
  end
  progress("Done!\n\n")
end

#harvest_file(filename) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zlocalize/harvester.rb', line 71

def harvest_file(filename)
  @new_entries = TranslationEntryCollection.new
  collect_entries(filename,@rails_root,false)

  @translation_file = ZLocalize::TranslationFile.new
  # merge

  @translation_file.load(File.join(@rails_root,@options[:output]))
  @translation_file.entries.synchronize_with(@new_entries,@options[:purge])
  File.open(File.join(@rails_root,@options[:output]),"w") do |f|
    f.write(@translation_file.to_yaml)
  end
end

#harvest_path(filespec) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/zlocalize/harvester.rb', line 33

def harvest_path(filespec)
  progress("Harvesting localizable strings from #{filespec}:\n")
  Dir.glob(File.join(@rails_root,filespec)) do |f|
    progress('.')
    collect_entries(f,@rails_root,is_erb?(f))
  end
  progress("\n")
end

#is_erb?(filename) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/zlocalize/harvester.rb', line 42

def is_erb?(filename)
  ['.erb','.rhtml'].include?(File.extname(filename))
end

#progress(msg) ⇒ Object



29
30
31
# File 'lib/zlocalize/harvester.rb', line 29

def progress(msg)
   print msg unless @options[:silent]
end