Module: GetText::RubyParser

Defined in:
lib/gettext/parser/ruby.rb

Constant Summary collapse

ID =
['gettext', '_', 'N_', 'sgettext', 's_']
PLURAL_ID =
['ngettext', 'n_', 'Nn_']

Class Method Summary collapse

Class Method Details

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

:nodoc:



130
131
132
133
# File 'lib/gettext/parser/ruby.rb', line 130

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

.parse_lines(file_name, lines, targets) ⇒ Object

:nodoc:



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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/gettext/parser/ruby.rb', line 135

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
  msgid = nil
  line_no = nil
  
  rl.parse do |tk|
    case tk
    when RubyToken::TkIDENTIFIER, RubyToken::TkCONSTANT
      if ID.include?(tk.name)
        target = :normal
      elsif PLURAL_ID.include?(tk.name)
        target = :plural
      else
        target = nil
      end
      line_no = tk.line_no.to_s
    when RubyToken::TkSTRING
      if target
        if msgid
          msgid += tk.value
        else
          msgid = tk.value 
        end
      end
    when RubyToken::TkPLUS, RubyToken::TkNL
      #do nothing
    when RubyToken::TkCOMMA
      if msgid and target == :plural
        msgid += "\000"
        target = :normal
      end
    else
      if msgid
        key_existed = targets.assoc(msgid)
        if key_existed 
          targets[targets.index(key_existed)] = key_existed <<
            file_name + ":" + line_no
        else
          targets << [msgid.gsub(/\n/, '\n'), file_name + ":" + line_no]
        end
        msgid = nil
        target = nil
      end
    end
    targets
  end
  
  targets
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


191
192
193
# File 'lib/gettext/parser/ruby.rb', line 191

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