Module: GetText::RubyParser

Extended by:
RubyParser
Included in:
RubyParser
Defined in:
lib/gettext/parser/ruby.rb,
lib/gettext/tools/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(path, deprecated = []) ⇒ Object

(Since 2.1.0) the 2nd parameter is deprecated (and ignored here). And You don’t need to keep the pomessages as unique.



73
74
75
76
# File 'lib/gettext/parser/ruby.rb', line 73

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

#parse_lines(path, lines, deprecated = []) ⇒ Object

:nodoc:



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
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gettext/parser/ruby.rb', line 78

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|
    begin
      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
        elsif MSGCTXT_PLURAL_ID.include?(tk.name)
          target = :msgctxt_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
          case target
          when :plural
            msgid += "\000"
            target = :normal
          when :msgctxt
            msgid += "\004"
            target = :normal
          when :msgctxt_plural
            msgid += "\004"
            target = :plural
          else
            target = :normal
          end
        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
    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 1
    end
  end
  targets
end

#target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


154
155
156
# File 'lib/gettext/parser/ruby.rb', line 154

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