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:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gettext/tools/xgettext.rb', line 75

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

  @input_files = nil
  @output = nil

  @package_name = nil
  @package_version = nil
  @msgid_bugs_address = nil
  @copyright_holder = nil
  @output_encoding = nil

  @parse_options = {}
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:



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

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:



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

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

.run(*arguments) ⇒ Object



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

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.



137
138
139
# File 'lib/gettext/tools/xgettext.rb', line 137

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

#check_command_line_options(*options) ⇒ Object

:nodoc:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/gettext/tools/xgettext.rb', line 190

def check_command_line_options(*options) # :nodoc:
  input_files, output = parse_arguments(*options)

  if input_files.empty?
    raise ArgumentError, _("no input files")
  end

  output ||= STDOUT

  @input_files = input_files
  @output = output

  @package_name ||= "PACKAGE"
  @package_version ||= "VERSION"
  @msgid_bugs_address ||= ""
  @copyright_holder ||= "THE PACKAGE'S COPYRIGHT HOLDER"
end

#generate_pot(paths) ⇒ Object

:nodoc:



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

def generate_pot(paths) # :nodoc:
  po = parse(paths)
  entries = []
  po.each do |target|
    entries << encode(target.to_s)
  end
  entries.join("\n")
end

#generate_pot_headerObject

:nodoc:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gettext/tools/xgettext.rb', line 141

def generate_pot_header # :nodoc:
  time = now.strftime("%Y-%m-%d %H:%M%z")

  <<EOH
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR #{@copyright_holder}
# This file is distributed under the same license as the #{@package_name} package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: #{@package_name} #{@package_version}\\n"
"Report-Msgid-Bugs-To: #{@msgid_bugs_address}\\n"
"POT-Creation-Date: #{time}\\n"
"PO-Revision-Date: #{time}\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: LANGUAGE <[email protected]>\\n"
"Language: \\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=#{@output_encoding}\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"
EOH
end

#parse(paths) ⇒ Object

:nodoc:



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gettext/tools/xgettext.rb', line 176

def parse(paths) # :nodoc:
  po = []
  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

#parse_arguments(*options) ⇒ Object

:nodoc:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/gettext/tools/xgettext.rb', line 208

def parse_arguments(*options) #:nodoc:
  output = nil

  parser = OptionParser.new
  banner = _("Usage: %s input.rb [-r parser.rb] [-o output.pot]") % $0
  parser.banner = banner
  parser.separator("")
  description = _("Extract translatable strings from given input files.")
  parser.separator(description)
  parser.separator("")
  parser.separator(_("Specific options:"))

  parser.on("-o", "--output=FILE",
            _("write output to specified file")) do |out|
    output = out
  end

  parser.on("--package-name=PACKAGE",
            _("set package name in output")) do |out|
    @package_name = out
  end

  parser.on("--package-version=VERSION",
            _("set package version in output")) do |out|
    @package_version = out
  end

  parser.on("--msgid-bugs-address=EMAIL",
            _("set report address for msgid bugs")) do |out|
    @msgid_bugs_address = out
  end

  parser.on("--copyright-holder=STRING",
            _("set copyright holder in output")) do |out|
    @copyright_holder = out
  end

  parser.on("--output-encoding=ENCODING",
            _("set encoding for output")) do |encoding|
    @output_encoding = encoding
  end

  parser.on("-r", "--require=library",
            _("require the library before executing xgettext")) do |out|
    require out
  end

  parser.on("-c", "--add-comments[=TAG]",
            _("If TAG is specified, place comment blocks starting with TAG and precedding keyword lines in output file"),
            _("If TAG is not specified, place all comment blocks preceing keyword lines in output file"),
            _("(default: %s)") % _("no TAG")) do |tag|
    @parse_options[:comment_tag] = tag
  end

  parser.on("-d", "--debug", _("run in debugging mode")) do
    $DEBUG = true
  end

  parser.on("-h", "--help", _("display this help and exit")) do
    puts(parser.help)
    exit(true)
  end

  parser.on_tail("--version", _("display version information and exit")) do
    puts(GetText::VERSION)
    exit(true)
  end

  parser.parse!(options)

  [options, output]
end

#run(*options) ⇒ Object

:nodoc:



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/gettext/tools/xgettext.rb', line 281

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

  @output_encoding ||= "UTF-8"
  pot = generate_pot_header
  pot << "\n"
  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