Module: GetText

Included in:
ActionView::Helpers::ActiveRecordHelper::L10n, ActionView::Helpers::DateHelper, ActiveRecord::Base, ActiveRecord::Errors, ActiveRecord::Migration, ActiveRecord::Migration, ActiveRecord::RecordInvalid, ActiveRecordParser, ActiveRecordParser, Container, ErbContainer, GladeParser, RGetText, RMsgMerge, RMsgfmt, Rails
Defined in:
lib/gettext/version.rb,
lib/gettext.rb,
lib/gettext/cgi.rb,
lib/gettext/erb.rb,
lib/gettext/rails.rb,
lib/gettext/utils.rb,
lib/gettext/rmsgfmt.rb,
lib/gettext/poparser.rb,
lib/gettext/rgettext.rb,
lib/gettext/container.rb,
lib/gettext/rmsgmerge.rb,
lib/gettext/rmsgmerge.rb,
lib/gettext/rmsgmerge.rb,
lib/gettext/parser/erb.rb,
lib/gettext/textdomain.rb,
lib/gettext/parser/ruby.rb,
lib/gettext/parser/glade.rb,
lib/gettext/textdomainmanager.rb,
lib/gettext/parser/active_record.rb

Overview

version - version information of Ruby-GetText-Package

Copyright (C) 2005-2007 Masao Mutoh

You may redistribute it and/or modify it under the same
license terms as Ruby.

Defined Under Namespace

Modules: ActiveRecordParser, Container, ErbContainer, ErbParser, GladeParser, RGetText, RMsgMerge, RMsgfmt, Rails, RubyParser Classes: NoboundTextDomainError, PoParser, TextDomain, TextDomainManager

Constant Summary collapse

VERSION =
"1.90.0"
@@__cached =
! $DEBUG
@@__textdomainmanagers =
Hash.new
@@__cache_target_classes =
{}
@@__cache_bound_target =
{}
@@__cache_bound_targets =
{}
@@__cache_msgids =
{}
@@__cache_nmsgids =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._Object

call-seq:

gettext(msgid)
_(msgid)

Translates msgid and return the message.

  • msgid: the message id.

  • Returns: localized text by msgid. If there are not binded mo-file, it will return msgid.

:nodoc:



524
525
526
# File 'lib/gettext.rb', line 524

def gettext(msgid)
  sgettext(msgid, nil)
end

.bindtextdomain(domainname, options = {}, locale_ = nil, charset = nil) ⇒ Object

call-seq: bindtextdomain(domainname, options = {})

Bind a textdomain(%path/%locale/LC_MESSAGES/%domainname.mo) to your program. Normally, the texdomain scope becomes a ruby-script-file. So you need to call this function each ruby-script-files. On the other hand, if you call this function under GetText::Container (gettext/container, gettext/erb, gettext/rails), the textdomain scope becomes a Class/Module.

  • domainname: the textdomain name.

  • options: options as an Hash.

    • :path - the path to the mo-files. When the value is nil, it will search default paths such as /usr/share/locale, /usr/local/share/locale)

    • :locale - the locale string such as “ja_JP.UTF-8”. Generally, you should use GetText.set_locale instead. The value is searched order by: the value of this value > System default language.

    • :charset - output charset. This affect the current textdomain only. Generally, you should use GetText.set_output_charset instead. The value is searched order by: the value of Locale.set_output_charset > ENV > this value > System default charset.

  • Returns: the GetText::TextDomainManager.

Note: Don’t use locale_, charset argument(not in options). They are remained for backward compatibility.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gettext.rb', line 82

def bindtextdomain(domainname, options = {}, locale_ = nil, charset = nil)
  opt = {}
  if options.kind_of? String
    # For backward compatibility
    opt = {:path => options, :locale => locale_, :charset => charset}
  elsif options
    opt = options
  end
  opt[:locale] = opt[:locale] ? Locale::Object.new(opt[:locale]) : Locale.get
  opt[:charset] = TextDomainManager.output_charset if TextDomainManager.output_charset
  opt[:locale].charset = opt[:charset] if opt[:charset]
  Locale.set_current(opt[:locale])
  target_key = bound_target
  manager = @@__textdomainmanagers[target_key]
  if manager
    manager.set_locale(opt[:locale]) 
  else
    manager = TextDomainManager.new(target_key, opt[:locale])
    @@__textdomainmanagers[target_key] = manager
  end
  manager.add_textdomain(domainname, opt)
  manager
end

.bound_target(klass = self) ⇒ Object

:nodoc:



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/gettext.rb', line 199

def bound_target(klass = self) # :nodoc:
  if cached?
    if @@__cache_bound_target[klass]
      return @@__cache_bound_target[klass]
    end
  end

  ret = nil
  if klass.kind_of? Class or klass.kind_of? Module
    ret = klass
  else
    ret = klass.class
  end
  ret = GetText if ret.name =~ /^\#<|^$/
  @@__cache_bound_target[klass] = ret
  ret
end

.bound_targets(klass) ⇒ Object

:nodoc:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/gettext.rb', line 219

def bound_targets(klass)  # :nodoc:
  if cached?
    if @@__cache_bound_targets[klass]
      return @@__cache_bound_targets[klass]
    end
  end
  ret = []
  klass = bound_target(klass)
  ary = klass.name.split(/::/)
  while(v = ary.shift)
    ret.unshift(((ret.size == 0) ? eval(v) : ret[0].const_get(v)))
  end
  @@__cache_bound_targets[klass] = ((ret + klass.ancestors + [GetText]) & @@__textdomainmanagers.keys).uniq
end

.cached=(val) ⇒ Object

Set the value whether cache messages or not. true to cache messages, otherwise false.

Default is true. If $DEBUG is false, messages are not checked even if this value is true.



39
40
41
42
# File 'lib/gettext.rb', line 39

def cached=(val)
  @@__cached = val
  GetText::TextDomain.check_mo = ! val
end

.cached?Boolean

Return the cached value.

Returns:

  • (Boolean)


45
46
47
# File 'lib/gettext.rb', line 45

def cached?
  @@__cached
end

.cgiObject

Gets the CGI object. If it is nil, returns new CGI object.

  • Returns: the CGI object



36
37
38
# File 'lib/gettext/cgi.rb', line 36

def cgi
  Locale.cgi
end

.cgi=(cgi_) ⇒ Object

Same as GetText.set_cgi.

  • cgi_: CGI object

  • Returns: cgi_



29
30
31
32
# File 'lib/gettext/cgi.rb', line 29

def cgi=(cgi_)
  set_cgi(cgi_)
  cgi_
end

.clear_cacheObject

Clear the cached messages.



50
51
52
53
54
55
56
# File 'lib/gettext.rb', line 50

def clear_cache
  @@__cache_msgids = {}
  @@__cache_nmsgids = {}
  @@__cache_target_classes = {}
  @@__cache_bound_target = {}
  @@__cache_bound_targets = {}
end

.create_mofiles(verbose = false, podir = "./po", targetdir = "./data/locale", targetpath_rule = "%s/LC_MESSAGES") ⇒ Object

Move to gettext/utils.rb. This will be removed in the feature. This remains for backward compatibility. Use gettext/utils.rb instead.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gettext/utils.rb', line 89

def create_mofiles(verbose = false, 
     podir = "./po", targetdir = "./data/locale", 
     targetpath_rule = "%s/LC_MESSAGES") 

  modir = File.join(targetdir, targetpath_rule)
  Dir.glob(File.join(podir, "*/*.po")) do |file|
    lang, basename = /\/([^\/]+?)\/(.*)\.po/.match(file[podir.size..-1]).to_a[1,2]
    outdir = modir % lang
    FileUtils.mkdir_p(outdir) unless File.directory?(outdir)
    rmsgfmt(file, File.join(outdir, "#{basename}.mo"))
    if verbose
	$stderr.puts %Q[#{file} -> #{File.join(outdir, "#{basename}.mo")}]
    end
  end
end

.current_textdomain_info(options = {}) ⇒ Object

Show the current textdomain information. This function is for debugging.

  • options: options as a Hash.

    • :with_messages - show informations with messages of the current mo file. Default is false.

    • :out - An output target. Default is STDOUT.

    • :with_paths - show the load paths for mo-files.



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/gettext.rb', line 501

def current_textdomain_info(options = {})
  opts = {:with_messages => false, :with_paths => false, :out => STDOUT}.merge(options)
  ret = nil
  each_textdomain {|textdomain|
    opts[:out].puts "TextDomain name: \"#{textdomain.name}\""
    opts[:out].puts "TextDomain current locale: \"#{textdomain.current_locale}\""
    opts[:out].puts "TextDomain current mo filename: \"#{textdomain.current_mo.filename}\""
    if opts[:with_paths]
	opts[:out].puts "TextDomain locale file paths:"
	textdomain.locale_paths.each do |v|
 opts[:out].puts "  #{v}"
	end
    end
    if opts[:with_messages]
	opts[:out].puts "The messages in the mo file:"
	textdomain.current_mo.each{|k, v|
 opts[:out].puts "  \"#{k}\": \"#{v}\""
	}
    end
  }
end

.each_textdomain(klass = self, ignore_targets = []) ⇒ Object

Iterates bound textdomains.

  • klass: a class/module to find. Default is the class of self.

  • ignore_targets: Ignore tragets.

  • Returns: a bound GetText::TextDomain or nil.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/gettext.rb', line 156

def each_textdomain(klass = self, ignore_targets = []) #:nodoc:
  bound_targets(klass).each do |target|
    unless ignore_targets.include? target
	manager = @@__textdomainmanagers[target]
	if manager
 manager.each{ |textdomain|
   yield textdomain
 }
	end
    end
  end
  self
end

.find_targets(klass) ⇒ Object

:nodoc:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/gettext.rb', line 172

def find_targets(klass)  #:nodoc:
  if cached?
    return @@__cache_target_classes[klass] if @@__cache_target_classes[klass]
  end
  unless (klass.kind_of? Class or klass.kind_of? Module)
    klass = klass.class
  end
  ret = []
  ary = klass.name.split(/::/)
  while(v = ary.shift)
    if ret.size == 0
      if v.kind_of? Class
        target = v
      else
        target = eval(v)
        return [GetText] unless (target = eval(v)) # For anonymous module
      end
    else
      target = ret[0].const_get(v)
    end
    ret.unshift(target) if target
  end
  @@__cache_target_classes[klass] = ret.size > 0 ? ret : [klass]
end

.gettext(msgid) ⇒ Object

call-seq:

gettext(msgid)
_(msgid)

Translates msgid and return the message.

  • msgid: the message id.

  • Returns: localized text by msgid. If there are not binded mo-file, it will return msgid.



243
244
245
# File 'lib/gettext.rb', line 243

def gettext(msgid)
  sgettext(msgid, nil)
end

.included(mod) ⇒ Object

:nodoc:



28
29
30
# File 'lib/gettext.rb', line 28

def self.included(mod)  #:nodoc:
  mod.extend self
end

.localeObject

Gets the current locale.

  • Returns: a current Locale::Object



484
485
486
# File 'lib/gettext.rb', line 484

def locale
  Locale.current
end

.locale=(locale) ⇒ Object

Sets the default/current locale. This method haves the strongest infulence. All of the Textdomains are set the new locale.

Note that you shouldn’t use this for your own Libraries.

  • locale: a locale string or Locale::Object

  • Returns: a locale string



454
455
456
457
458
# File 'lib/gettext.rb', line 454

def locale=(locale)
  Locale.default = locale
  set_locale_all(locale)
  Locale.default
end

.msgmerge(defpo, refpo, app_version) ⇒ Object

Merges two Uniforum style .po files together.

Note This function requires “msgmerge” tool included in GNU GetText. So you need to install GNU GetText.

The def.po file is an existing PO file with translations which will be taken over to the newly created file as long as they still match; comments will be preserved, but extracted comments and file positions will be discarded.

The ref.pot file is the last created PO file with up-to-date source references but old translations, or a PO Template file (generally created by rgettext); any translations or comments in the file will be discarded, however dot comments and file positions will be preserved. Where an exact match cannot be found, fuzzy matching is used to produce better results.

Usually you don’t need to call this function directly. Use GetText.update_pofiles instead.

  • defpo: a po-file. translations referring to old sources

  • refpo: a po-file. references to new sources

  • app_version: the application information which appears “Project-Id-Version: #app_version” in the pot/po-files.

  • Returns: self



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gettext/utils.rb', line 44

def msgmerge(defpo, refpo, app_version) 
  $stderr.puts defpo
  cmd = ENV["MSGMERGE_PATH"] || "msgmerge"

  cont = ""
  if FileTest.exist? defpo
    cont = `#{cmd} #{defpo} #{refpo}`
  else
    File.open(refpo) do |io| 
	cont = io.read
    end
  end
  if cont.empty?
    failed_filename = refpo + "~"
    FileUtils.cp(refpo, failed_filename)
    $stderr.puts _("Failed to merge with %{defpo}") % {:defpo => defpo}
    $stderr.puts _("New .pot was copied to %{failed_filename}") %{:failed_filename => failed_filename}
    raise _("`#{cmd}' may not be found. \nInstall GNU Gettext then set PATH or MSGMERGE_PATH correctly.")
  else
    cont.sub!(/(Project-Id-Version\:).*$/, "\\1 #{app_version}\\n\"")
    File.open(defpo, "w") do |out|
      out.write(cont)
    end
  end
  self
end

.msgmerge_all(textdomain, app_version, po_root = "po", refpot = "tmp.pot") ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
# File 'lib/gettext/utils.rb', line 71

def msgmerge_all(textdomain, app_version, po_root = "po", refpot = "tmp.pot") # :nodoc:
  FileUtils.mkdir_p(po_root) unless FileTest.exist? po_root
  msgmerge("#{po_root}/#{textdomain}.pot", refpot, app_version)
  
  Dir.glob("#{po_root}/*/#{textdomain}.po"){ |f|
    lang = /#{po_root}\/(.*)\//.match(f).to_a[1]
    msgmerge("#{po_root}/#{lang}/#{textdomain}.po", refpot, app_version)
  }
end

.N_(msgid) ⇒ Object

This function does nothing. But it is required in order to recognize the msgid by rgettext.

  • msgid: the message id.

  • Returns: msgid.



310
311
312
# File 'lib/gettext.rb', line 310

def N_(msgid)
  msgid
end

.n_Object

call-seq:

ngettext(msgid, msgid_plural, n)
ngettext(msgids, n)  # msgids = [msgid, msgid_plural]
n_(msgid, msgid_plural, n)
n_(msgids, n)  # msgids = [msgid, msgid_plural]

The ngettext is similar to the gettext function as it finds the message catalogs in the same way. But it takes two extra arguments for plural form.

  • msgid: the singular form.

  • msgid_plural: the plural form.

  • n: a number used to determine the plural form.

  • Returns: the localized text which key is msgid_plural if n is plural(follow plural-rule) or msgid. “plural-rule” is defined in po-file.

:nodoc:



525
526
527
# File 'lib/gettext.rb', line 525

def ngettext(arg1, arg2, arg3 = nil)
  nsgettext(arg1, arg2, arg3, nil)
end

.ngettext(arg1, arg2, arg3 = nil) ⇒ Object

call-seq:

ngettext(msgid, msgid_plural, n)
ngettext(msgids, n)  # msgids = [msgid, msgid_plural]
n_(msgid, msgid_plural, n)
n_(msgids, n)  # msgids = [msgid, msgid_plural]

The ngettext is similar to the gettext function as it finds the message catalogs in the same way. But it takes two extra arguments for plural form.

  • msgid: the singular form.

  • msgid_plural: the plural form.

  • n: a number used to determine the plural form.

  • Returns: the localized text which key is msgid_plural if n is plural(follow plural-rule) or msgid. “plural-rule” is defined in po-file.



303
304
305
# File 'lib/gettext.rb', line 303

def ngettext(arg1, arg2, arg3 = nil)
  nsgettext(arg1, arg2, arg3, nil)
end

.ns_Object

call-seq:

nsgettext(msgid, msgid_plural, n, div = "|")
nsgettext(msgids, n, div = "|")  # msgids = [msgid, msgid_plural]
n_(msgid, msgid_plural, n, div = "|")
n_(msgids, n, div = "|")  # msgids = [msgid, msgid_plural]

The nsgettext is similar to the ngettext. But if there are no localized text, it returns a last part of msgid separeted “div”.

  • msgid: the singular form with “div”. (e.g. “Special|An apple”)

  • msgid_plural: the plural form. (e.g. “%num Apples”)

  • n: a number used to determine the plural form.

  • Returns: the localized text which key is msgid_plural if n is plural(follow plural-rule) or msgid. “plural-rule” is defined in po-file.

:nodoc:



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/gettext.rb', line 527

def nsgettext(arg1, arg2, arg3 = "|", arg4 = "|")
  if arg1.kind_of?(Array)
    msgid = arg1[0]
    msgid_plural = arg1[1]
    n = arg2
    if arg3 and arg3.kind_of? Numeric
      raise ArgumentError, _("3rd parmeter is wrong: value = %{number}") % {:number => arg3}
    end
    div = arg3
  else
    msgid = arg1
    msgid_plural = arg2
    n = arg3
    div = arg4
  end

  cached_key = [bound_target, Locale.current, msgid + "\000" + msgid_plural]
  msgs = nil
  if @@__cached
    if @@__cache_nmsgids.has_key?(cached_key)
      msgs = @@__cache_nmsgids[cached_key]  # [msgstr, cond_as_string]
    end
  end
  unless msgs
    # Use "for"(not "each") to support JRuby 1.1.0.
    for target in bound_targets(self)
      manager = @@__textdomainmanagers[target]
      for textdomain in manager.textdomains
        msgs = textdomain[1].ngettext_data(msgid, msgid_plural)
        break if msgs
      end
      break if msgs
    end
    msgs = [[msgid, msgid_plural], "n != 1"] unless msgs
    @@__cache_nmsgids[cached_key] = msgs
  end
  msgstrs = msgs[0]
  if div and msgstrs[0] == msgid
    if index = msgstrs[0].rindex(div)
	msgstrs[0] = msgstrs[0][(index + 1)..-1]
    end
  end
  plural = eval(msgs[1])
  if plural.kind_of?(Numeric)
    ret = msgstrs[plural]
  else
    ret = plural ? msgstrs[1] : msgstrs[0]
  end
  ret
end

.nsgettext(arg1, arg2, arg3 = "|", arg4 = "|") ⇒ Object

call-seq:

nsgettext(msgid, msgid_plural, n, div = "|")
nsgettext(msgids, n, div = "|")  # msgids = [msgid, msgid_plural]
n_(msgid, msgid_plural, n, div = "|")
n_(msgids, n, div = "|")  # msgids = [msgid, msgid_plural]

The nsgettext is similar to the ngettext. But if there are no localized text, it returns a last part of msgid separeted “div”.

  • msgid: the singular form with “div”. (e.g. “Special|An apple”)

  • msgid_plural: the plural form. (e.g. “%num Apples”)

  • n: a number used to determine the plural form.

  • Returns: the localized text which key is msgid_plural if n is plural(follow plural-rule) or msgid. “plural-rule” is defined in po-file.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/gettext.rb', line 338

def nsgettext(arg1, arg2, arg3 = "|", arg4 = "|")
  if arg1.kind_of?(Array)
    msgid = arg1[0]
    msgid_plural = arg1[1]
    n = arg2
    if arg3 and arg3.kind_of? Numeric
      raise ArgumentError, _("3rd parmeter is wrong: value = %{number}") % {:number => arg3}
    end
    div = arg3
  else
    msgid = arg1
    msgid_plural = arg2
    n = arg3
    div = arg4
  end

  cached_key = [bound_target, Locale.current, msgid + "\000" + msgid_plural]
  msgs = nil
  if @@__cached
    if @@__cache_nmsgids.has_key?(cached_key)
      msgs = @@__cache_nmsgids[cached_key]  # [msgstr, cond_as_string]
    end
  end
  unless msgs
    # Use "for"(not "each") to support JRuby 1.1.0.
    for target in bound_targets(self)
      manager = @@__textdomainmanagers[target]
      for textdomain in manager.textdomains
        msgs = textdomain[1].ngettext_data(msgid, msgid_plural)
        break if msgs
      end
      break if msgs
    end
    msgs = [[msgid, msgid_plural], "n != 1"] unless msgs
    @@__cache_nmsgids[cached_key] = msgs
  end
  msgstrs = msgs[0]
  if div and msgstrs[0] == msgid
    if index = msgstrs[0].rindex(div)
	msgstrs[0] = msgstrs[0][(index + 1)..-1]
    end
  end
  plural = eval(msgs[1])
  if plural.kind_of?(Numeric)
    ret = msgstrs[plural]
  else
    ret = plural ? msgstrs[1] : msgstrs[0]
  end
  ret
end

.output_charsetObject

Gets the current output_charset which is set using GetText.set_output_charset.

  • Returns: output_charset.



478
479
480
# File 'lib/gettext.rb', line 478

def output_charset
  TextDomainManager.output_charset || locale.charset
end

.output_charset=(charset) ⇒ Object

Same as GetText.set_output_charset

  • charset: an output_charset

  • Returns: charset



472
473
474
# File 'lib/gettext.rb', line 472

def output_charset=(charset)
  TextDomainManager.output_charset = charset
end

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

Creates a po-file from targetfiles(ruby-script-files, ActiveRecord, .rhtml files, glade-2 XML files), then output the result to out. If no parameter is set, it behaves same as command line tools(rgettet).

This function is a part of GetText.create_pofiles. Usually you don’t need to call this function directly.

Note for ActiveRecord, you need to run your database server and configure the config/database.xml correctly before execute this function.

  • targetfiles: An Array of po-files or nil.

  • out: output IO or output path.

  • Returns: self



259
260
261
262
# File 'lib/gettext/rgettext.rb', line 259

def rgettext(targetfiles = nil, out = STDOUT)
  RGetText.run(targetfiles, out)
  self
end

.rmsgfmt(targetfile = nil, output_path = nil) ⇒ Object

Creates a mo-file from a targetfile(po-file), then output the result to out. If no parameter is set, it behaves same as command line tools(rmsgfmt).

  • targetfile: An Array of po-files or nil.

  • output_path: output path.

  • Returns: the MOFile object.



79
80
81
# File 'lib/gettext/rmsgfmt.rb', line 79

def rmsgfmt(targetfile = nil, output_path = nil)
  RMsgfmt.run(targetfile, output_path)
end

.rmsgmerge(reference = nil, definition = nil, out = STDOUT) ⇒ Object

Experimental



489
490
491
# File 'lib/gettext/rmsgmerge.rb', line 489

def rmsgmerge(reference = nil, definition = nil, out = STDOUT)
  RMsgMerge.run(reference, definition, out)
end

.s_Object

call-seq:

sgettext(msgid, div = '|')
s_(msgid, div = '|')

Translates msgid, but if there are no localized text, it returns a last part of msgid separeted “div”.

  • msgid: the message id.

  • div: separator or nil.

  • Returns: the localized text by msgid. If there are no localized text, it returns a last part of msgid separeted “div”.

See: www.gnu.org/software/gettext/manual/html_mono/gettext.html#SEC151 :nodoc:



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
# File 'lib/gettext.rb', line 526

def sgettext(msgid, div = '|')
  cached_key = [bound_target, Locale.current, msgid]
  if cached?
    if @@__cache_msgids[cached_key]
      return @@__cache_msgids[cached_key]
    end
  end
  msg = nil

  # Use "for"(not "each") to support JRuby 1.1.0.
  for target in bound_targets(self)
    manager = @@__textdomainmanagers[target]
    for textdomain in manager.textdomains
      msg = textdomain[1].gettext(msgid)
      break if msg
    end
    break if msg
  end

  msg ||= msgid
  if div and msg == msgid
    if index = msg.rindex(div)
	msg = msg[(index + 1)..-1]
    end
  end
  @@__cache_msgids[cached_key] = msg
end

.set_cgi(cgi_) ⇒ Object

Sets a CGI object.

  • cgi_: CGI object

  • Returns: self



22
23
24
# File 'lib/gettext/cgi.rb', line 22

def set_cgi(cgi_)
  Locale.set_cgi(cgi_)
end

.set_locale(locale, this_target_only = false) ⇒ Object

Sets the current locale to the current class/module

Notice that you shouldn’t use this for your own Libraries.

  • locale: a locale string or Locale::Object.

  • this_target_only: true if you want to change the current class/module only.

Otherwise, this changes the locale of the current class/module and its ancestors. Default is false.

  • Returns: self



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/gettext.rb', line 397

def set_locale(locale, this_target_only = false)
  ret = nil
  if locale
    if locale.kind_of? Locale::Object
	ret = locale
    else
	ret = Locale::Object.new(locale.to_s)
    end
    ret.charset = TextDomainManager.output_charset if TextDomainManager.output_charset
    Locale.set(ret)
  else
    Locale.set(nil)
    ret = Locale.get
  end
  if this_target_only
    manager = @@__textdomainmanagers[bound_target]
    if manager
	manager.set_locale(ret, ! cached?)
    end
  else
    each_textdomain {|textdomain|
      textdomain.set_locale(ret, ! cached?)
    }
  end
  self
end

.set_locale_all(locale) ⇒ Object

Sets current locale to the all textdomains.

Note that you shouldn’t use this for your own Libraries.

  • locale: a locale string or Locale::Object, otherwise nil to use default locale.

  • Returns: self



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/gettext.rb', line 429

def set_locale_all(locale)
  ret = nil
  if locale
    if locale.kind_of? Locale::Object
	ret = locale
    else
	ret = Locale::Object.new(locale.to_s)
    end
  else
    ret = Locale.default
  end
  ret.charset = TextDomainManager.output_charset if TextDomainManager.output_charset
  Locale.set_current(ret)
  TextDomainManager.each_all {|textdomain|
    textdomain.set_locale(ret, ! cached?)
  }
  self
end

.set_output_charset(charset) ⇒ Object

Sets charset(String) such as “euc-jp”, “sjis”, “CP932”, “utf-8”, … You shouldn’t use this in your own Libraries.

  • charset: an output_charset

  • Returns: charset



464
465
466
467
# File 'lib/gettext.rb', line 464

def set_output_charset(charset)
  TextDomainManager.output_charset = charset
  self
end

.setlocaleObject

Sets the default/current locale. This method haves the strongest infulence. All of the Textdomains are set the new locale.

Note that you shouldn’t use this for your own Libraries.

  • locale: a locale string or Locale::Object

  • Returns: a locale string

:nodoc:



523
524
525
526
527
# File 'lib/gettext.rb', line 523

def locale=(locale)
  Locale.default = locale
  set_locale_all(locale)
  Locale.default
end

.sgettext(msgid, div = '|') ⇒ Object

call-seq:

sgettext(msgid, div = '|')
s_(msgid, div = '|')

Translates msgid, but if there are no localized text, it returns a last part of msgid separeted “div”.

  • msgid: the message id.

  • div: separator or nil.

  • Returns: the localized text by msgid. If there are no localized text, it returns a last part of msgid separeted “div”.

See: www.gnu.org/software/gettext/manual/html_mono/gettext.html#SEC151



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/gettext.rb', line 259

def sgettext(msgid, div = '|')
  cached_key = [bound_target, Locale.current, msgid]
  if cached?
    if @@__cache_msgids[cached_key]
      return @@__cache_msgids[cached_key]
    end
  end
  msg = nil

  # Use "for"(not "each") to support JRuby 1.1.0.
  for target in bound_targets(self)
    manager = @@__textdomainmanagers[target]
    for textdomain in manager.textdomains
      msg = textdomain[1].gettext(msgid)
      break if msg
    end
    break if msg
  end

  msg ||= msgid
  if div and msg == msgid
    if index = msg.rindex(div)
	msg = msg[(index + 1)..-1]
    end
  end
  @@__cache_msgids[cached_key] = msg
end

.textdomain(domainname) ⇒ Object

Binds a existed textdomain to your program. This is the same function with GetText.bindtextdomain but simpler(and faster) than bindtextdomain. Notice that you need to call GetText.bindtextdomain first. If the domainname hasn’t bound yet, raises GetText::NoboundTextDomainError.

  • domainname: a textdomain name.

  • Returns: the GetText::TextDomainManager.



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/gettext.rb', line 125

def textdomain(domainname)
  domain = TextDomainManager.textdomain(domainname)
  raise NoboundTextDomainError, "#{domainname} is not bound." unless domain
  target_key = bound_target
  manager = @@__textdomainmanagers[target_key]
  unless manager
    manager = TextDomainManager.new(target_key, Locale.get)
    @@__textdomainmanagers[target_key] = manager
  end
  manager.set_locale(Locale.get)
  manager.add_textdomain(domainname)
  manager
end

.update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot") ⇒ Object

At first, this creates the #po_root/#domainname.pot file using GetText.rgettext. Since 2nd time, this updates(merges) the #po_root/#domainname.pot and all of the #po_root/#lang/#domainname.po files under “po_root” using “msgmerge”.

Note “msgmerge” tool is included in GNU GetText. So you need to install GNU GetText.

See <HOWTO maintain po/mo files(www.yotabanana.com/hiki/ruby-gettext-howto-manage.html)> for more detals.

  • domainname: the textdomain name.

  • targetfiles: An Array of target files or nil (See GetText.rgettext for more details).

  • app_version: the application information which appears “Project-Id-Version: #app_version” in the pot/po-files.

  • po_root: the root directory of po-files.

  • refpot: set the temporary file name. You shouldn’t use this(It will be removed).

(e.g.) GetText.update_pofiles("myapp", Dir.glob("lib/*.rb"), "myapp 1.0.0")


120
121
122
123
124
# File 'lib/gettext/utils.rb', line 120

def update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot")
  rgettext(files, refpot)
  msgmerge_all(textdomain, app_version, po_root, refpot)
  File.delete(refpot)
end

Instance Method Details

#add_default_locale_path(path) ⇒ Object

Add default locale path.

  • path: a new locale path. (e.g.) “/usr/share/locale/%locale/LC_MESSAGES/%name.mo” (‘locale’ => “ja_JP”, ‘name’ => “textdomain”)

  • Returns: the new DEFAULT_LOCALE_PATHS



492
493
494
# File 'lib/gettext.rb', line 492

def add_default_locale_path(path)
  TextDomain.add_default_locale_path(path)
end

#bindtextdomain_to(klass, domainname, options = {}) ⇒ Object

Includes GetText module and bind a textdomain to a class.

  • klass: the target ruby class.

  • domainname: the textdomain name.

  • options: options as an Hash. See GetText.bindtextdomain.



110
111
112
113
114
115
116
117
# File 'lib/gettext.rb', line 110

def bindtextdomain_to(klass, domainname, options = {}) 
  ret = nil
  klass.module_eval {
    include GetText
    ret = bindtextdomain(domainname, options)
  }
  ret
end

#Nn_(msgid, msgid_plural) ⇒ Object

This is same function as N_ but for ngettext.

  • msgid: the message id.

  • msgid_plural: the plural message id.

  • Returns: msgid.



318
319
320
# File 'lib/gettext.rb', line 318

def Nn_(msgid, msgid_plural)
  [msgid, msgid_plural]
end

#textdomain_to(klass, domainname) ⇒ Object

Includes GetText module and bind an exsited textdomain to a class. See textdomain for more detail.

  • klass: the target ruby class.

  • domainname: the textdomain name.



143
144
145
146
147
148
149
150
# File 'lib/gettext.rb', line 143

def textdomain_to(klass, domainname) 
  ret = nil
  klass.module_eval {
    include GetText
    ret = textdomain(domainname)
  }
  ret
end