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 =
{ :models => true, :controllers => true, :views => true, :helpers => true, :output => 'config/locales/app-strings.yml', :overwrite_existing => false, :add_paths => [], :silent => false, :purge => false }
- HARVEST_SECTIONS =
{ :models => [ "app/models/**/*.rb" ], :controllers => [ "app/controllers/**/*.rb" ], :helpers => [ "app/helpers/**/*.rb" ], :views => [ "app/views/**/*.erb", "app/views/**/*.rhtml" ], :lib => [ "lib/**/*.rb"] }
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_section(section, filespecs) ⇒ 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.
36 37 38 39 |
# File 'lib/zlocalize/harvester.rb', line 36 def initialize(rails_root, = {}) @rails_root = rails_root @options = DEFAULT_HARVEST_OPTIONS.merge() end |
Instance Attribute Details
#rails_root ⇒ Object
Returns the value of attribute rails_root.
19 20 21 |
# File 'lib/zlocalize/harvester.rb', line 19 def rails_root @rails_root end |
Instance Method Details
#collect_entries(filename, root, is_erb = false) ⇒ Object
collect entries in a source file
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/zlocalize/harvester.rb', line 105 def collect_entries(filename,root,is_erb = false) sp = ZLocalize::SourceParser.new(filename,root,is_erb) sp.parse 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
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zlocalize/harvester.rb', line 61 def harvest @new_entries = TranslationEntryCollection.new HARVEST_SECTIONS.each_pair do |section,paths| harvest_section(section,paths) progress("\n") end if @options[:add_paths].is_a?(Array) && @options.size > 0 harvest_section("additional paths",@options[:add_paths]) 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
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/zlocalize/harvester.rb', line 91 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_section(section, filespecs) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/zlocalize/harvester.rb', line 45 def harvest_section(section,filespecs) progress("Harvesting localizable strings from #{section}:\n") filespecs.each do |spec| progress("#{spec}: ") Dir.glob(File.join(@rails_root,spec)) do |f| progress('.') collect_entries(f,@rails_root,is_erb?(f)) end progress("\n") end end |
#is_erb?(filename) ⇒ Boolean
57 58 59 |
# File 'lib/zlocalize/harvester.rb', line 57 def is_erb?(filename) ['.erb','.rhtml'].include?(File.extname(filename)) end |
#progress(msg) ⇒ Object
41 42 43 |
# File 'lib/zlocalize/harvester.rb', line 41 def progress(msg) print msg unless @options[:silent] end |