Class: GetText::RubyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext/tools/parser/ruby.rb

Defined Under Namespace

Classes: POExtractor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ RubyParser

Returns a new instance of RubyParser.

Examples:

:comment_tag option: String tag

path = "hello.rb"
# content:
#   # TRANSLATORS: This is a comment to translators.
#   _("Hello")
#
#   # This is a comment for programmers.
#   # TRANSLATORS: This is a comment to translators.
#   # This is also a comment to translators.
#   _("World")
#
#   # This is a comment for programmers.
#   # This is also a comment for programmers
#   # because all lines don't start with "TRANSRATORS:".
#   _("Bye")
options = {:comment_tag => "TRANSLATORS:"}
parser = GetText::RubyParser.new(path, options)
parser.parse
# => [
#   POEntry<
#     :msgid => "Hello",
#     :extracted_comment =>
#       "TRANSLATORS: This is a comment to translators.",
#   >,
#   POEntry<
#     :msgid => "World",
#     :extracted_comment =>
#       "TRANSLATORS: This is a comment to translators.\n" +
#       "This is also a comment to translators.",
#   >,
#   POEntry<
#     :msgid => "Bye",
#     :extracted_comment => nil,
#   >,
# ]

:comment_tag option: nil tag

path = "hello.rb"
# content:
#   # This is a comment to translators.
#   # This is also a comment for translators.
#   _("Hello")
options = {:comment_tag => nil}
parser = GetText::RubyParser.new(path, options)
parser.parse
# => [
#   POEntry<
#     :msgid => "Hello",
#     :extracted_comment =>
#       "This is a comment to translators.\n" +
#       " This is also a comment for translators.",
#   >,
# ]

Parameters:

  • path (String)

    Ruby script path to be parsed

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :comment_tag (String, nil)

    The tag to detect comments to be extracted. The extracted comments are used to deliver messages to translators from programmers.

    If the tag is String and a line in a comment start with the tag, the line and the following lines are extracted.

    If the tag is nil, all comments are extracted.



419
420
421
422
# File 'lib/gettext/tools/parser/ruby.rb', line 419

def initialize(path, options={})
  @path = path
  @options = options
end

Class Method Details

.parse(path, options = {}) ⇒ Array<POEntry>

Parses Ruby script located at path.

This is a short cut method. It equals to new(path, options).parse.

Parameters:

  • path (String)

    Ruby script path to be parsed

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :comment_tag (String, nil)

    The tag to detect comments to be extracted. The extracted comments are used to deliver messages to translators from programmers.

    If the tag is String and a line in a comment start with the tag, the line and the following lines are extracted.

    If the tag is nil, all comments are extracted.

Returns:

  • (Array<POEntry>)

    Extracted messages

See Also:



348
349
350
351
# File 'lib/gettext/tools/parser/ruby.rb', line 348

def parse(path, options={})
  parser = new(path, options)
  parser.parse
end

.target?(file) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


334
335
336
# File 'lib/gettext/tools/parser/ruby.rb', line 334

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

Instance Method Details

#detect_encoding(source) ⇒ Object



436
437
438
439
440
441
442
443
# File 'lib/gettext/tools/parser/ruby.rb', line 436

def detect_encoding(source)
  binary_source = source.dup.force_encoding("ASCII-8BIT")
  if /\A.*coding\s*[=:]\s*([[:alnum:]\-_]+)/ =~ binary_source
    $1.gsub(/-(?:unix|mac|dos)\z/, "")
  else
    nil
  end
end

#parseArray<POEntry>

Extracts messages from @path.

Returns:

  • (Array<POEntry>)

    Extracted messages



427
428
429
430
431
432
433
434
# File 'lib/gettext/tools/parser/ruby.rb', line 427

def parse
  source = IO.read(@path)

  encoding = detect_encoding(source) || source.encoding
  source.force_encoding(encoding)

  parse_source(source)
end

#parse_source(source) ⇒ Object



445
446
447
448
449
450
451
452
# File 'lib/gettext/tools/parser/ruby.rb', line 445

def parse_source(source)
  extractor = POExtractor.new(source, @path)
  if @options.key?(:comment_tag)
    extractor.use_comment = true
    extractor.comment_tag = @options[:comment_tag]
  end
  extractor.parse([])
end