Module: GetText::RGetText

Extended by:
GetText
Defined in:
lib/gettext/tools/rgettext.rb

Overview

:nodoc:

Constant Summary collapse

VERSION =

constant values

GetText::VERSION

Constants included from GetText

BOM_UTF8

Class 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, rgettext, rmsgfmt, rmsgmerge, set_cgi, set_current_locale, set_locale, set_output_charset, sgettext, textdomain, textdomain_to, update_pofiles, update_pofiles_org

Class Method Details

.add_parser(klass) ⇒ Object

Add an option parser the option parser module requires to have target?(file) and parser(file, ary) method.

require 'gettext/tools/rgettext'
module FooParser
  module_function
  def target?(file)
    File.extname(file) == '.foo'  # *.foo file only.
  end
  def parse(file)
    :
    ary = []
    # Simple message
    po = PoMessage.new(:normal)
    po.msgid = "hello"
    po.sources = ["foo.rb:200", "bar.rb:300"]
    po.add_comment("Comment for the message")
    ary << po
    # Plural message
    po = PoMessage.new(:plural)
    po.msgid = "An apple"
    po.msgid_plural = "Apples"
    po.sources = ["foo.rb:200", "bar.rb:300"]
    ary << po
    # Simple message with the message context
    po = PoMessage.new(:msgctxt)
    po.msgctxt = "context"
    po.msgid = "hello"
    po.sources = ["foo.rb:200", "bar.rb:300"]
    ary << po
    # Plural message with the message context.
    po = PoMessage.new(:msgctxt_plural)
    po.msgctxt = "context"
    po.msgid = "An apple"
    po.msgid_plural = "Apples"
    po.sources = ["foo.rb:200", "bar.rb:300"]
    ary << po
    return ary
  end
end

GetText::RGetText.add_parser(FooParser)


89
90
91
# File 'lib/gettext/tools/rgettext.rb', line 89

def add_parser(klass)
  @ex_parsers.insert(0, klass)
end

.check_optionsObject

:nodoc:



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/gettext/tools/rgettext.rb', line 168

def check_options # :nodoc:
  output = STDOUT

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

  opts.on("-o", "--output=FILE", _("write output to specified file")) do |out|
    unless FileTest.exist? out
      output = File.new(File.expand_path(out), "w+")
    else
      $stderr.puts(_("File '%s' already exists.") % out)
      exit 1
    end
  end

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

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

  opts.on_tail("--version", _("display version information and exit")) do
    puts "#{$0} #{VERSION}"
    puts "#{File.join(Config::CONFIG["bindir"], Config::CONFIG["RUBY_INSTALL_NAME"])} #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
    exit
  end

  opts.parse!(ARGV)

  if ARGV.size == 0
    puts opts.help
    exit 1
  end

  [ARGV, output]
end

.generate_pot(paths) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
127
# File 'lib/gettext/tools/rgettext.rb', line 120

def generate_pot(paths) # :nodoc:
  pomessages = parse(paths)
  str = ""
  pomessages.each do |target|
    str << target.to_po_str.force_encoding('UTF-8')
  end
  str
end

.generate_pot_headerObject

:nodoc:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gettext/tools/rgettext.rb', line 93

def generate_pot_header # :nodoc:
  time = Time.now.strftime("%Y-%m-%d %H:%M")
  off = Time.now.utc_offset
  sign = off <= 0 ? '-' : '+'
  time += sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))

  "# SOME DESCRIPTIVE TITLE.\n# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n# This file is distributed under the same license as the PACKAGE package.\n# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n#\n#, fuzzy\nmsgid \"\"\nmsgstr \"\"\n\"Project-Id-Version: PACKAGE VERSION\\\\n\"\n\"POT-Creation-Date: \#{time}\\\\n\"\n\"PO-Revision-Date: \#{time}\\\\n\"\n\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\\\n\"\n\"Language-Team: LANGUAGE <[email protected]>\\\\n\"\n\"MIME-Version: 1.0\\\\n\"\n\"Content-Type: text/plain; charset=UTF-8\\\\n\"\n\"Content-Transfer-Encoding: 8bit\\\\n\"\n\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\\\n\"\n"
end

.parse(paths) ⇒ Object

:nodoc:



129
130
131
132
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
158
159
160
161
162
163
164
165
166
# File 'lib/gettext/tools/rgettext.rb', line 129

def parse(paths) # :nodoc:
  pomessages = []
  paths = [paths] if paths.kind_of? String
  paths.each do |path|
    begin
      @ex_parsers.each do |klass|
        if klass.target?(path)
          if klass.method(:parse).arity == 1
            targets = klass.parse(path)
          else
            # For backward compatibility.
            targets = klass.parse(path, [])
          end

          targets.each{|pomessage|
            if pomessage.kind_of? Array
              pomessage = PoMessage.new_from_ary(pomessage)
            end

            # Save the previous target.
            existing = pomessages.empty? ? nil : pomessages.index(pomessages.find {|t| t == pomessage})
            if existing
              pomessage = pomessages[existing].merge(pomessage)
              pomessages[existing] = pomessage
            else
              pomessages << pomessage
            end
          }
          break
        end
      end
    rescue
      puts _("Error parsing %{path}") % {:path => path}
      raise
    end
  end
  pomessages
end

.run(paths = nil, out = STDOUT) ⇒ Object

:nodoc:



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/gettext/tools/rgettext.rb', line 211

def run(paths = nil, out = STDOUT)  # :nodoc:
  if paths.is_a? String
    paths = [paths]
  elsif ! paths
    paths, out = check_options
  end
  if paths.size == 0
    raise ArgumentError, _("no input files")
  end
  if out.is_a? String
    File.open(File.expand_path(out), "w+") do |file|
      file.puts generate_pot_header
      file.puts generate_pot(paths)
    end
  else
    out.puts generate_pot_header
    out.puts generate_pot(paths)
  end
  self
end