Class: ZLocalize::SourceProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(filename, root, is_erb = false) ⇒ SourceProcessor

Returns a new instance of SourceProcessor.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/zlocalize/source_processor.rb', line 10

def initialize(filename, root, is_erb = false)
  @in_hash = 0
  @translate_calls = []
  @root = File.join(File.expand_path(root).downcase,'/') # add a trailing /
  @filename = File.expand_path(filename).downcase
  @relative_filename = @filename.gsub(@root,'')
  content = File.open(filename, "r") { |f| f.read }
  if is_erb
    content = ActionView::Template::Handlers::ERB::Erubi.new(content, escape: true, trim: true).src
  end
  @parser = create_parser_for_ruby_version
  begin
    @stree = @parser.parse(content)
    process(@stree)
  rescue ArgumentError => ae
    raise ArgumentError.new("In #{filename} #{ae.message}")
  end
end

Instance Method Details

#create_parser_for_ruby_versionObject



29
30
31
32
33
34
35
36
# File 'lib/zlocalize/source_processor.rb', line 29

def create_parser_for_ruby_version
  md = RUBY_VERSION.match(/^(\d)\.(\d)/)
  begin
    kls = Object.const_get('Parser').const_get("Ruby#{md[1]}#{md[2]}")
  rescue
    raise "Unsupported Ruby version #{RUBY_VERSION}"
  end
end

#get_string_array_node_value(node) ⇒ Object



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

def get_string_array_node_value(node)
  unless node.is_a?(AST::Node) || node.type != :array
    raise ArgumentError.new("On line #{node.loc.selector.line} at column #{node.loc.selector.column+1} : Array expected but got: #{node.inspect}")
  end
  a = []
  for i in 0..node.children.size - 1
    a << get_string_node_value(node.children[i])
  end
  a
end

#get_string_node_value(node) ⇒ Object



64
65
66
67
68
69
# File 'lib/zlocalize/source_processor.rb', line 64

def get_string_node_value(node)
  unless node.is_a?(AST::Node) && node.type  == :str
    raise ArgumentError.new("On line #{node.loc.selector.line} at column #{node.loc.selector.column+1} : String Expected but got: #{node.inspect}")
  end
  return node.children[0]
end

#is_zlocalize_const?(node) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/zlocalize/source_processor.rb', line 60

def is_zlocalize_const?(node)
  return node.is_a?(AST::Node) && node.type == :const && node.children[1] == :ZLocalize
end

#make_translation_entry(h) ⇒ Object



102
103
104
105
106
# File 'lib/zlocalize/source_processor.rb', line 102

def make_translation_entry(h)
  TranslationEntry.new('plural'     => h[:name] == 'n_' || h[:name] == 'pluralize',
                       'source'     => h[:parameter],
                       'references' => [ "#{@relative_filename}:#{h[:line_no]}" ])
end

#process(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/zlocalize/source_processor.rb', line 38

def process(node)
  return unless node.is_a?(AST::Node)
  if node.type == :send
    if node.children[0] == nil
      if node.children[1] == :_
        process_translate_call(node) and return
      elsif node.children[1] == :n_
        process_pluralize_call(node) and return
      end
    elsif is_zlocalize_const?(node)
      if node.children[1] == :translate
        process_translate_call(node) and return
      elsif node.children[1] == :pluralize
        process_pluralize_call(node) and return
      end
    end
  end
  node.children.each do |n|
    process(n)
  end
end

#process_pluralize_call(node) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/zlocalize/source_processor.rb', line 92

def process_pluralize_call(node)
  @translate_calls << { name: node.children[1].to_s,
                        line_no: node.loc.selector.line,
                        char_no: node.loc.selector.column+1,
                        parameter: get_string_array_node_value(node.children[2]) }
  for i in 3..node.children.size-1
    process(node.children[i])
  end
end

#process_translate_call(node) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/zlocalize/source_processor.rb', line 82

def process_translate_call(node)
  @translate_calls << { name: node.children[1].to_s,
                        line_no: node.loc.selector.line,
                        char_no: node.loc.selector.column+1,
                        parameter: get_string_node_value(node.children[2]) }
  for i in 3..node.children.size-1
    process(node.children[i])
  end
end

#translation_entriesObject

return a Hash of all translation entries we collected



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/zlocalize/source_processor.rb', line 109

def translation_entries
  entries = {}
  @translate_calls.each do |c|
    e = make_translation_entry(c)
    if entries[e.source]
      entries[e.source].references += e.references
    else
      entries[e.source] = e
    end
  end
  entries
end