Class: PutText::Parser::Ruby

Inherits:
Base
  • Object
show all
Defined in:
lib/puttext/parser/ruby.rb

Constant Summary collapse

METHODS =
{
  gettext:   :regular,
  _:         :regular,
  N_:        :regular,
  ngettext:  :plural,
  n_:        :plural,
  Nn_:       :plural,
  sgettext:  :context_sep,
  s_:        :context_sep,
  nsgettext: :context_sep_plural,
  ns_:       :context_sep_plural,
  pgettext:  :context,
  p_:        :context,
  npgettext: :context_plural,
  np_:       :context_plural
}.freeze
PARAMS =
{
  regular:            %i(msgid),
  plural:             %i(msgid msgid_plural),
  context:            %i(msgctxt msgid),
  context_plural:     %i(msgctxt msgid msgid_plural),
  context_sep:        %i(msgid separator),
  context_sep_plural: %i(msgid msgid_plural _ separator)
}.freeze

Instance Method Summary collapse

Methods inherited from Base

#strings_from_file

Constructor Details

#initializeRuby

Returns a new instance of Ruby.



41
42
43
# File 'lib/puttext/parser/ruby.rb', line 41

def initialize
  @ruby_parser = ::Parser::CurrentRuby.new
end

Instance Method Details

#strings_from_source(source, filename: '(string)', first_line: 1) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puttext/parser/ruby.rb', line 45

def strings_from_source(source, filename: '(string)', first_line: 1)
  buffer = ::Parser::Source::Buffer.new(filename, first_line)
  buffer.source = source

  @ruby_parser.reset
  ast = @ruby_parser.parse(buffer)

  if ast.is_a? ::Parser::AST::Node
    find_strings_in_ast(ast)
  else
    []
  end
end