Class: GetText::Tools::XGetText

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

Constant Summary collapse

@@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:



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

def initialize #:nodoc:
  @parsers = []

  @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,
    use_one_line_per_reference: false,
  }
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:



72
73
74
# File 'lib/gettext/tools/xgettext.rb', line 72

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:



41
42
43
# File 'lib/gettext/tools/xgettext.rb', line 41

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

.run(*arguments) ⇒ Object



31
32
33
# File 'lib/gettext/tools/xgettext.rb', line 31

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.



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

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

#parse(paths) ⇒ Object

:nodoc:



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

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:



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

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