Class: GetText::Tools::XGetText

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/gettext/tools/xgettext.rb

Constant Summary

@@default_parsers =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

[]

Constants included from GetText

VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

#N_, #Nn_, #bindtextdomain, #bindtextdomain_to, #cgi, #cgi=, #gettext, included, #locale, #ngettext, #npgettext, #nsgettext, #output_charset, #pgettext, #set_cgi, #set_current_locale, #set_locale, #set_output_charset, #sgettext, #textdomain, #textdomain_to

Constructor Details

#initializeXGetText

:nodoc:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gettext/tools/xgettext.rb', line 76

def initialize #:nodoc:
  @parsers = @@default_parsers.dup

  @input_files = nil
  @output = nil

  @package_name = "PACKAGE"
  @package_version = "VERSION"
  @msgid_bugs_address = ""
  @copyright_holder = "THE PACKAGE'S COPYRIGHT HOLDER"
  @copyright_year = "YEAR"
  @output_encoding = "UTF-8"

  @parse_options = {}

  @po_order = :references
  @po_format_options = {
    :max_line_width => POEntry::Formatter::DEFAULT_MAX_LINE_WIDTH,
  }
end

Instance Attribute Details

#parse_optionsHash<Symbol, Object> (readonly)

Returns Options for parsing. Options are depend on each parser.

Returns:

  • (Hash<Symbol, Object>)

    Options for parsing. Options are depend on each parser.

See Also:



74
75
76
# File 'lib/gettext/tools/xgettext.rb', line 74

def parse_options
  @parse_options
end

Class Method Details

.add_parser(parser) ⇒ void

This method returns an undefined value.

Adds a parser to the default parser list.

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.

See Also:



43
44
45
# File 'lib/gettext/tools/xgettext.rb', line 43

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

.run(*arguments) ⇒ Object



33
34
35
# File 'lib/gettext/tools/xgettext.rb', line 33

def run(*arguments)
  new.run(*arguments)
end

Instance Method Details

#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.



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

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

#parse(paths) ⇒ Object

:nodoc:



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gettext/tools/xgettext.rb', line 163

def parse(paths) # :nodoc:
  po = PO.new
  paths = [paths] if paths.kind_of?(String)
  paths.each do |path|
    begin
      parse_path(path, po)
    rescue
      puts(_("Error parsing %{path}") % {:path => path})
      raise
    end
  end
  po
end

#run(*options) ⇒ Object

:nodoc:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/gettext/tools/xgettext.rb', line 148

def run(*options)  # :nodoc:
  check_command_line_options(*options)

  pot = generate_pot(@input_files)

  if @output.is_a?(String)
    File.open(File.expand_path(@output), "w+") do |file|
      file.puts(pot)
    end
  else
    @output.puts(pot)
  end
  self
end