Method: GetText::Tools::XGetText#add_parser

Defined in:
lib/gettext/tools/xgettext.rb

#add_parser(parser) ⇒ void

This method returns an undefined value.

The parser object requires to have target?(path) and parse(path) method.

Examples:

How to add your parser

require "gettext/tools/xgettext"
class FooParser
  def target?(path)
    File.extname(path) == ".foo"  # *.foo file only.
  end
  def parse(path, options={})
    po = []
    # Simple entry
    entry = POEntry.new(:normal)
    entry.msgid = "hello"
    entry.references = ["foo.rb:200", "bar.rb:300"]
    entry.add_comment("Comment for the entry")
    po << entry
    # Plural entry
    entry = POEntry.new(:plural)
    entry.msgid = "An apple"
    entry.msgid_plural = "Apples"
    entry.references = ["foo.rb:200", "bar.rb:300"]
    po << entry
    # Simple entry with the entry context
    entry = POEntry.new(:msgctxt)
    entry.msgctxt = "context"
    entry.msgid = "hello"
    entry.references = ["foo.rb:200", "bar.rb:300"]
    po << entry
    # Plural entry with the message context.
    entry = POEntry.new(:msgctxt_plural)
    entry.msgctxt = "context"
    entry.msgid = "An apple"
    entry.msgid_plural = "Apples"
    entry.references = ["foo.rb:200", "bar.rb:300"]
    po << entry
    return po
  end
end

GetText::Tools::XGetText.add_parser(FooParser.new)

Parameters:

  • parser (#target?, #parse)

    It parses target file and extracts translate target entries from the target file. If there are multiple target files, parser.parse is called multiple times.



143
144
145
# File 'lib/gettext/tools/xgettext.rb', line 143

def add_parser(parser)
  @parsers.unshift(parser)
end