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_']
MSGCTXT_PLURAL_ID =
['npgettext', 'np_']

Class Method Summary collapse

Class Method Details

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

:nodoc:



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

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

.parse_lines(file_name, lines, targets) ⇒ Object

:nodoc:



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

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
        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
    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 1
  end
  targets
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


152
153
154
# File 'lib/gettext/parser/ruby.rb', line 152

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