Module: GetText::RubyParser

Extended by:
RubyParser
Included in:
RubyParser
Defined in:
lib/gettext/parser/ruby.rb

Constant Summary collapse

ID =
['gettext', '_', 'N_', 'sgettext', 's_']
PLURAL_ID =
['ngettext', 'n_', 'Nn_', 'ns_', 'nsgettext']
MSGCTXT_ID =
['pgettext', 'p_']
MSGCTXT_PLURAL_ID =
['npgettext', 'np_']

Instance Method Summary collapse

Instance Method Details

#parse(file, targets = []) ⇒ Object

:nodoc:



91
92
93
94
# File 'lib/gettext/parser/ruby.rb', line 91

def parse(file, targets = [])  # :nodoc:
  lines = IO.readlines(file)
  parse_lines(file, lines, targets)
end

#parse_lines(file_name, lines, targets) ⇒ Object

:nodoc:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gettext/parser/ruby.rb', line 96

def parse_lines(file_name, lines, targets)  # :nodoc:
  file = StringIO.new(lines.join + "\n")
  rl = RubyLexX.new
  rl.set_input(file)
  rl.skip_space = true
  #rl.readed_auto_clean_up = true

  target = nil
  line_no = nil
  last_extracted_comment = ''
  reset_extracted_comment = false
  rl.parse do |tk|
    begin
      case tk
      when RubyToken::TkIDENTIFIER, RubyToken::TkCONSTANT
        if ID.include?(tk.name)
          target = TranslationTarget.new(:normal)
        elsif PLURAL_ID.include?(tk.name)
          target = TranslationTarget.new(:plural)
        elsif MSGCTXT_ID.include?(tk.name)
          target = TranslationTarget.new(:msgctxt)
        elsif MSGCTXT_PLURAL_ID.include?(tk.name)
          target = TranslationTarget.new(:msgctxt_plural)
        else
          target = nil
        end
        line_no = tk.line_no.to_s
      when RubyToken::TkSTRING
        target.set_current_attribute tk.value if target
      when RubyToken::TkPLUS, RubyToken::TkNL
        #do nothing
      when RubyToken::TkCOMMA
        target.advance_to_next_attribute if target
      else
        if target && target.msgid
          existing=targets.find {|t| t.matches?(target)}
          existing=targets.index(existing) if existing
          if existing
            target = targets[existing].merge(target)
            targets[existing] = target
          else
            targets << target
          end
          target.occurrences << file_name + ":" + line_no
          target.add_extracted_comment last_extracted_comment unless last_extracted_comment.empty?
          target = nil
        end
      end
    rescue
      $stderr.print "\n\nError"
      $stderr.print " parsing #{file_name}:#{tk.line_no}\n\t #{lines[tk.line_no - 1]}" if tk
      $stderr.print "\n #{$!.inspect} in\n"
      $stderr.print $!.backtrace.join("\n")
      $stderr.print "\n"
      exit 1
    end

    case tk 
    when RubyToken::TkCOMMENT_WITH_CONTENT
      last_extracted_comment = "" if reset_extracted_comment
      if last_extracted_comment.empty?
        # new comment from programmer to translator?
        comment1 = tk.value.lstrip
        if comment1 =~ /^TRANSLATORS\:/
          last_extracted_comment += $'
        end
      else
        last_extracted_comment += "\n" 
        last_extracted_comment += tk.value
      end
      reset_extracted_comment = false
    when RubyToken::TkNL
    else
      reset_extracted_comment = true
    end
  end
  targets
end

#target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


175
176
177
# File 'lib/gettext/parser/ruby.rb', line 175

def target?(file)  # :nodoc:
  true # always true, as default parser.
end