Class: ZLocalize::IdentifierExpression

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

Overview

Basic identifier expression

Instance Attribute Summary collapse

Attributes inherited from Expression

#char_no, #line_no, #sub_method, #text

Instance Method Summary collapse

Methods inherited from Expression

#is_translate_call, #prefix, #set_text

Constructor Details

#initialize(line_no, char_no, name) ⇒ IdentifierExpression

Returns a new instance of IdentifierExpression.



62
63
64
65
66
67
# File 'lib/zlocalize/rdoc_source_parser.rb', line 62

def initialize(line_no,char_no,name)
  @name = name
  @parameters = []
  @sub_method = nil
  super(line_no,char_no)
end

Instance Attribute Details

#nameObject

:nodoc: all



59
60
61
# File 'lib/zlocalize/rdoc_source_parser.rb', line 59

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

Instance Method Details

#display(indent = 0) ⇒ Object

Used for debugging



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

def display(indent = 0)
  i = prefix(indent)
  i << "#{self.class.name} (#{line_no},#{char_no}): #{@name} (#{parameters.size} parameters)\n"
  @parameters.each { |p| i << p.display(indent+2) }
  unless @sub_method.nil?
    i << prefix(indent+1) + "sub-method:\n"
    i << @sub_method.display(indent+2)
  end
  i
end

#to_entry_stringObject



109
110
111
# File 'lib/zlocalize/rdoc_source_parser.rb', line 109

def to_entry_string
  "(identifier '#{name}')".force_encoding("UTF-8")
end

#to_translation_entry(filename) ⇒ Object

Convert ourselves to a Hash or an Array of Hashes which each represent a call to a translate method. Will only output something if this expression is an actual call to _ or n_



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/zlocalize/rdoc_source_parser.rb', line 84

def to_translation_entry(filename)
  if ['_','n_'].include?(@name)
    params = @parameters.dup
  elsif @name == 'ZLocalize' && @sub_method.is_a?(IdentifierExpression) && ['pluralize','translate'].include?(@sub_method.name)
    params = @sub_method.parameters.dup
  else
    return nil
  end
  if params.size > 0
    p1 = params.shift
    # we only collect litteral strings and Arrays
    entry = []
    if [StringExpression,ArrayExpression].include?(p1.class)
      entry << TranslationEntry.new( 'plural'     => @name == 'n_',
                                     'source'     => p1.to_entry_string,
                                     'references' => [ "#{filename}:#{p1.line_no}" ])
    end
    # collect all other parameters to this call, in case they are themselves calls to nested _("")
    params.each { |p| entry << p.to_translation_entry(filename) }
    return entry.flatten.compact
  else
    nil
  end
end