Module: GetText::RGetText

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

Overview

:nodoc:

Constant Summary collapse

VERSION =

constant values

GetText::VERSION
DATE =
%w($Date: 2008/07/26 06:59:34 $)[1]
MAX_LINE_LEN =
70

Constants included from GetText

CACHE_BOUND_TARGET_MAX_SIZE

Class Method Summary collapse

Methods included from GetText

N_, Nn_, _, add_default_locale_path, bindtextdomain, bindtextdomain_to, bound_target, bound_targets, cached=, cached?, cgi, cgi=, clear_cache, create_mofiles, current_textdomain_info, each_textdomain, find_targets, gettext, included, locale, locale=, msgmerge, msgmerge_all, n_, ngettext, npgettext, ns_, nsgettext, output_charset, output_charset=, p_, pgettext, remove_all_textdomains, rgettext, rmsgfmt, rmsgmerge, s_, set_cgi, set_locale, set_locale_all, set_output_charset, setlocale, sgettext, textdomain, textdomain_to, update_pofiles

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/rgettext'
module FooParser
  module_function
  def target?(file)
    File.extname(file) == '.foo'  # *.foo file only.
  end
  def parse(file, ary)
    :
    return ary # [["msgid1", "foo.rb:200"], ["msgid2", "bar.rb:300", "baz.rb:400"], ...]
  end
end

GetText::RGetText.add_parser(FooParser)


66
67
68
# File 'lib/gettext/rgettext.rb', line 66

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

.check_optionsObject

:nodoc:



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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/gettext/rgettext.rb', line 184

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} (#{DATE})"
	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(ary) ⇒ Object

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gettext/rgettext.rb', line 95

def generate_pot(ary) # :nodoc:
  str = ""
  result = Array.new
  ary.each do |key|
	msgid = key.shift
	curr_pos = MAX_LINE_LEN
	key.each do |e|
	  if curr_pos + e.size > MAX_LINE_LEN
 str << "\n#:"
 curr_pos = 3
	  else
 curr_pos += (e.size + 1)
	  end
	  str << " " << e
	end
	msgid.gsub!(/"/, '\"')
	msgid.gsub!(/\r/, '')
	if msgid.include?("\000")
	  ids = msgid.split(/\000/)
	  str << "\nmsgid \"" << ids[0] << "\"\n"
	  str << "msgid_plural \"" << ids[1] << "\"\n"
	  str << "msgstr[0] \"\"\n"
	  str << "msgstr[1] \"\"\n"
	elsif msgid.include?("\004")
	  ids = msgid.split(/\004/)
	  str << "\nmsgctxt \"" << ids[0] << "\"\n"
	  str << "msgid \"" << ids[1] << "\"\n"
	  str << "msgstr \"\"\n"
	else
	  str << "\nmsgid \"" << msgid << "\"\n"
	  str << "msgstr \"\"\n"
	end
  end
  str
end

.generate_pot_headerObject

:nodoc:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gettext/rgettext.rb', line 70

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))

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

.normalize(ary) ⇒ Object

:nodoc:



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
# File 'lib/gettext/rgettext.rb', line 131

def normalize(ary)  # :nodoc:
  used_plural_msgs = []
  ary.select{|item| item[0].include? "\000"}.each do |plural_msg|
    next if used_plural_msgs.include?(plural_msg[0])

    key = plural_msg[0].split("\000")[0]

    ary.dup.each do |single_msg|
      if single_msg[0] == key or
          (single_msg[0] =~ /^#{Regexp.quote(key)}\000/ and 
           single_msg[0] != plural_msg[0])
        if single_msg[0] != key 
          warn %Q[Warning: n_("#{plural_msg[0].gsub(/\000/, '", "')}") and n_("#{single_msg[0].gsub(/\000/, '", "')}") are duplicated. First msgid was used.] 
            used_plural_msgs << single_msg[0]
        end

        single_msg[1..-1].each do |line_info|
          plural_msg << line_info
          fname = line_info.split(/:/)[0]
          sorted = plural_msg[1..-1].select{|l| l.split(/:/)[0].include?(fname)}.collect{|l| 
            aline = l.split(/:/)
            [aline[0], aline[1] =~ /[0-9]+/ ? aline[1].to_i : aline[1]]
          }.sort.collect{|l| "#{l[0]}:#{l[1]}"}
          plural_msg.delete_if{|i| sorted.include?(i)}
          sorted.each {|i|
            plural_msg << i
          }
        end
        ary.delete(single_msg)
      end
    end
  end
  ary.collect{|i| i.uniq}
end

.parse(files) ⇒ Object

:nodoc:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/gettext/rgettext.rb', line 166

def parse(files) # :nodoc:
  ary = []
  files.each do |file|
	begin
	  @ex_parsers.each do |klass|
 if klass.target?(file)
   ary = klass.parse(file, ary)
   break
 end
	  end
	rescue
	  puts "Error occurs in " + file
	  raise
	end
  end
  normalize(ary)
end

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

:nodoc:



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/gettext/rgettext.rb', line 227

def run(targetfiles = nil, out = STDOUT)  # :nodoc:
  if targetfiles.is_a? String
	targetfiles = [targetfiles]
  elsif ! targetfiles
	targetfiles, out = check_options
  end

  if targetfiles.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(parse(targetfiles))
	end
  else
	out.puts generate_pot_header
	out.puts generate_pot(parse(targetfiles))
  end
  self
end