Module: GetText::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_']

Class Method Summary collapse

Class Method Details

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

:nodoc:



69
70
71
72
# File 'lib/gettext/parser/ruby.rb', line 69

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

.parse_lines(file_name, lines, targets) ⇒ Object

:nodoc:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
# File 'lib/gettext/parser/ruby.rb', line 74

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
  tk = nil
  begin
	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
 elsif MSGCTXT_ID.include?(tk.name)
   target = :msgctxt
 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
   if target == :plural
     msgid += "\000"
   elsif target == :msgctxt
     msgid += "\004"
   end
   	      target = :normal
 end
	  else
 if msgid
   key_existed = targets.assoc(msgid.gsub(/\n/, '\n'))
   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
  rescue
	$stderr.print "\n\nError: #{$!.inspect} "
    $stderr.print " in #{file_name}:#{tk.line_no}\n\t #{lines[tk.line_no - 1]}" if tk
    $stderr.print "\n"
	exit
  end
  targets
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


142
143
144
# File 'lib/gettext/parser/ruby.rb', line 142

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