class GetText::Tools::XGetText
Attributes
parse_options[R]
@return [Hash<Symbol, Object>] Options for parsing. Options
are depend on each parser.
@see RubyParser#parse
@see ErbParser#parse
Public Class Methods
add_parser(parser)
click to toggle source
Adds a parser to the default parser list.
@param (see add_parser
) @return [void]
@see add_parser
# File lib/gettext/tools/xgettext.rb, line 42 def add_parser(parser) @@default_parsers.unshift(parser) end
run(*arguments)
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 32 def run(*arguments) new.run(*arguments) end
Public Instance Methods
add_parser(parser)
click to toggle source
The parser object requires to have target?(path) and parse(path) method.
@example 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)
@param [#target?, parse] parser
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.
@return [void]
# File lib/gettext/tools/xgettext.rb, line 143 def add_parser(parser) @parsers.unshift(parser) end
Private Instance Methods
create_po_entry(msgid, *references)
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 413 def create_po_entry(msgid, *references) type = :normal msgctxt = nil msgid_plural = nil if msgid.include?("\004") msgctxt, msgid = msgid.split(/\004/, 2) type = :msgctxt end if msgid.include?("\000") msgid, msgid_plural = msgid.split(/\000/, 2) if type == :msgctxt type = :msgctxt_plural else type = :plural end end po_entry = POEntry.new(type) po_entry.msgid = msgid po_entry.msgctxt = msgctxt po_entry.msgid_plural = msgid_plural po_entry.references = references po_entry end
header_comment()
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 181 def header_comment <<-COMMENT SOME DESCRIPTIVE TITLE. Copyright (C) #{@copyright_year} #{@copyright_holder} This file is distributed under the same license as the #{@package_name} package. FIRST AUTHOR <EMAIL@ADDRESS>, #{@copyright_year}. COMMENT end
header_content()
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 191 def header_content time = now.strftime("%Y-%m-%d %H:%M%z") <<-CONTENT Project-Id-Version: #{@package_name} #{@package_version} Report-Msgid-Bugs-To: #{@msgid_bugs_address} POT-Creation-Date: #{time} PO-Revision-Date: #{time} Last-Translator: FULL NAME <EMAIL@ADDRESS> Language-Team: LANGUAGE <LL@li.org> Language: MIME-Version: 1.0 Content-Type: text/plain; charset=#{@output_encoding} Content-Transfer-Encoding: 8bit Plural-Forms: nplurals=INTEGER; plural=EXPRESSION; CONTENT end
now()
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 177 def now Time.now end
parse_path(path, po)
click to toggle source
# File lib/gettext/tools/xgettext.rb, line 358 def parse_path(path, po) (@parsers + @@default_parsers).each do |parser| next unless parser.target?(path) # For backward compatibility if parser.method(:parse).arity == 1 or @parse_options.empty? extracted_po = parser.parse(path) else extracted_po = parser.parse(path, @parse_options) end extracted_po.each do |po_entry| if po_entry.kind_of?(Array) po_entry = create_po_entry(*po_entry) end if po_entry.msgid.empty? warn _("Warning: The empty \"\" msgid is reserved by " + "gettext. So gettext(\"\") doesn't returns " + "empty string but the header entry in po file.") # TODO: add pommesage.reference to the pot header as below: # # SOME DESCRIPTIVE TITLE. # # Copyright (C) YEAR THE COPYRIGHT HOLDER # # This file is distributed under the same license as the PACKAGE package. # # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. # # # #: test/test_gettext.rb:65 # #, fuzzy # "#: test/test_gettext.rb:65" line is added. next end if @output.is_a?(String) base_path = Pathname.new(@output).dirname.expand_path po_entry.references = po_entry.references.collect do |reference| path, line, = reference.split(/:(\d+)\z/, 2) absolute_path = Pathname.new(path).expand_path begin path = absolute_path.relative_path_from(base_path).to_s rescue ArgumentError raise # Should we ignore it? end "#{path}:#{line}" end end existing_entry = po[po_entry.msgctxt, po_entry.msgid] if existing_entry po_entry = existing_entry.merge(po_entry) end po[po_entry.msgctxt, po_entry.msgid] = po_entry end break end end