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

BOM_UTF8, MOFile, MoFile, PoParser, VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

#N_, #Nn_, #bindtextdomain, #bindtextdomain_to, #cgi, #cgi=, #create_mofiles, #create_mofiles_org, #gettext, included, #locale, #msgmerge, #ngettext, #npgettext, #nsgettext, #output_charset, #pgettext, #remove_bom, #set_cgi, #set_current_locale, #set_locale, #set_output_charset, #sgettext, #textdomain, #textdomain_to, #update_pofiles, #update_pofiles_org

Constructor Details

#initializeXGetText

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gettext/tools/xgettext.rb', line 69

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

  @input_files = nil
  @output = nil

  @package_name = nil
  @package_version = nil
  @msgid_bugs_address = nil
  @copyright_holder = nil
  @output_encoding = nil
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)
    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::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.



129
130
131
# File 'lib/gettext/tools/xgettext.rb', line 129

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

#check_command_line_options(*options) ⇒ Object

:nodoc:



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/gettext/tools/xgettext.rb', line 182

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:



159
160
161
162
163
164
165
166
# File 'lib/gettext/tools/xgettext.rb', line 159

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:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/gettext/tools/xgettext.rb', line 133

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:



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/gettext/tools/xgettext.rb', line 168

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:



200
201
202
203
204
205
206
207
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
# File 'lib/gettext/tools/xgettext.rb', line 200

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("-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:



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/gettext/tools/xgettext.rb', line 266

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