Class: ZLocalize::Harvester
- Inherits:
-
Object
- Object
- ZLocalize::Harvester
- 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
-
#rails_root ⇒ Object
Returns the value of attribute rails_root.
Instance Method Summary collapse
-
#collect_entries(filename, root, is_erb = false) ⇒ Object
collect entries in a source file.
- #harvest ⇒ Object
- #harvest_file(filename) ⇒ Object
- #harvest_path(filespec) ⇒ Object
-
#initialize(rails_root, options = {}) ⇒ Harvester
constructor
A new instance of Harvester.
- #is_erb?(filename) ⇒ Boolean
- #progress(msg) ⇒ Object
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, = {}) @rails_root = rails_root = { paths: ZLocalize.config.harvest_paths }.merge(DEFAULT_HARVEST_OPTIONS).merge().with_indifferent_access end |
Instance Attribute Details
#rails_root ⇒ Object
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 |
#harvest ⇒ Object
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 [:paths].each do |path| harvest_path(path) progress("\n") end @translation_file = ZLocalize::TranslationFile.new unless [:overwrite_existing] progress("Merging existing translations from #{@options[:output]}...\n") begin @translation_file.load(File.join(@rails_root,[:output])) rescue end end @translation_file.entries.synchronize_with(@new_entries,[:purge]) progress("Writing new translation file...\n") File.open(File.join(@rails_root,[: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,[:output])) @translation_file.entries.synchronize_with(@new_entries,[:purge]) File.open(File.join(@rails_root,[: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
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 [:silent] end |