Module: GetText

Included in:
ActionView::Helpers::ActiveRecordHelper::L10n, ActionView::Helpers::DateHelper, ActionView::Helpers::FormBuilder, 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-2008 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

CACHE_BOUND_TARGET_MAX_SIZE =

Max cached object_ids includes dead objects.

50000
VERSION =
"1.92.0"
@@__cached =

:nodoc:

! $DEBUG
@@__textdomainmanagers =
{}
@@__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. This doesn’t make a copy of the message.

You need to use String#dup if you want to modify the return value with destructive functions.

(e.g.1) _(“Hello ”).dup << “world”

But e.g.1 should be rewrite to:

(e.g.2) _(“Hello %val”) % => “world”

Because the translator may want to change the position of “world”.

  • msgid: the message id.

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

:nodoc:



589
590
591
# File 'lib/gettext.rb', line 589

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.



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

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:



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/gettext.rb', line 204

def bound_target(klass = self) # :nodoc:
  ret = (klass.kind_of? Module) ? klass : klass.class
  id = ret.object_id
  if cached?
   tgt = @@__cache_bound_target[id]
   return tgt if tgt
  end

  if ret.name =~ /^\#<|^$/ or ret == GetText
     #GC for dead object_ids.
     ret = Object
     if @@__cache_bound_target.size > CACHE_BOUND_TARGET_MAX_SIZE
        @@__cache_bound_target.clear
     end
  end
  @@__cache_bound_target[id] = ret
  ret
end

.bound_targets(klass) ⇒ Object

:nodoc:



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/gettext.rb', line 225

def bound_targets(klass)  # :nodoc:
  id = klass.object_id
  if cached?
    if @@__cache_bound_targets[id]
      return @@__cache_bound_targets[id]
    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[id] = ((ret + klass.ancestors + [Object]) & @@__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.



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

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

.cached?Boolean

Return the cached value.

Returns:

  • (Boolean)


48
49
50
# File 'lib/gettext.rb', line 48

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.



53
54
55
56
57
58
59
# File 'lib/gettext.rb', line 53

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.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gettext/utils.rb', line 96

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)
    $stderr.print %Q[#{file} -> #{File.join(outdir, "#{basename}.mo")} ... ] if verbose
    begin
      rmsgfmt(file, File.join(outdir, "#{basename}.mo"))
    rescue Exception => e
      $stderr.puts "Error." if verbose
      raise e
    end
    $stderr.puts "Done." if verbose
  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.



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/gettext.rb', line 560

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.



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/gettext.rb', line 159

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:



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

def find_targets(klass)  #:nodoc:
  unless (klass.kind_of? Class or klass.kind_of? Module)
    klass = klass.class
  end
  id = klass.object_id
  if cached?
    return @@__cache_target_classes[id] if @@__cache_target_classes[id]
  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 [Object] 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[id] = ret.size > 0 ? ret : [klass]
end

.gettext(msgid) ⇒ Object

call-seq:

gettext(msgid)
_(msgid)

Translates msgid and return the message. This doesn’t make a copy of the message.

You need to use String#dup if you want to modify the return value with destructive functions.

(e.g.1) _(“Hello ”).dup << “world”

But e.g.1 should be rewrite to:

(e.g.2) _(“Hello %val”) % => “world”

Because the translator may want to change the position of “world”.

  • msgid: the message id.

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



263
264
265
# File 'lib/gettext.rb', line 263

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



543
544
545
# File 'lib/gettext.rb', line 543

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



513
514
515
516
517
# File 'lib/gettext.rb', line 513

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gettext/utils.rb', line 47

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

  cont = ""
  if FileTest.exist? defpo
    `#{cmd} --version`
    unless $?.success?
      raise _("`#{cmd}' may not be found. \nInstall GNU Gettext then set PATH or MSGMERGE_PATH correctly.")
    end
    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 _("Check these po/pot-files. It may have syntax errors or something wrong.")
  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:



78
79
80
81
82
83
84
85
86
# File 'lib/gettext/utils.rb', line 78

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.



369
370
371
# File 'lib/gettext.rb', line 369

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:



590
591
592
# File 'lib/gettext.rb', line 590

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.



340
341
342
# File 'lib/gettext.rb', line 340

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:



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
# File 'lib/gettext.rb', line 593

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.



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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/gettext.rb', line 397

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.



537
538
539
# File 'lib/gettext.rb', line 537

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



531
532
533
# File 'lib/gettext.rb', line 531

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

.p_Object

call-seq:

pgettext(msgctxt, msgid)
p_(msgctxt, msgid)

Translates msgid with msgctxt. This methods is similer with s_().

e.g.) p_("File", "New")   == s_("File|New")
      p_("File", "Open")  == s_("File|Open")
  • msgctxt: the message context.

  • msgid: the message id.

  • Returns: the localized text by msgid. If there are no localized text, it returns msgid.

See: www.gnu.org/software/autoconf/manual/gettext/Contexts.html :nodoc:



592
593
594
# File 'lib/gettext.rb', line 592

def pgettext(msgctxt, msgid)
  sgettext(msgctxt + "\004" + msgid, "\004")
end

.pgettext(msgctxt, msgid) ⇒ Object

call-seq:

pgettext(msgctxt, msgid)
p_(msgctxt, msgid)

Translates msgid with msgctxt. This methods is similer with s_().

e.g.) p_("File", "New")   == s_("File|New")
      p_("File", "Open")  == s_("File|Open")
  • msgctxt: the message context.

  • msgid: the message id.

  • Returns: the localized text by msgid. If there are no localized text, it returns msgid.

See: www.gnu.org/software/autoconf/manual/gettext/Contexts.html



320
321
322
# File 'lib/gettext.rb', line 320

def pgettext(msgctxt, msgid)
  sgettext(msgctxt + "\004" + msgid, "\004")
end

.remove_all_textdomainsObject

for testing.



583
584
585
586
# File 'lib/gettext.rb', line 583

def remove_all_textdomains
  clear_cache
  @@__textdomainmanagers = {}
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



264
265
266
267
# File 'lib/gettext/rgettext.rb', line 264

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:



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/gettext.rb', line 591

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



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/gettext.rb', line 456

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



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/gettext.rb', line 488

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



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

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:



588
589
590
591
592
# File 'lib/gettext.rb', line 588

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



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/gettext.rb', line 279

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.



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gettext.rb', line 128

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


131
132
133
134
135
# File 'lib/gettext/utils.rb', line 131

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



551
552
553
# File 'lib/gettext.rb', line 551

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.



113
114
115
116
117
118
119
120
# File 'lib/gettext.rb', line 113

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.



377
378
379
# File 'lib/gettext.rb', line 377

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

#npgettext(msgctxt, arg1, arg2 = nil) ⇒ Object

call-seq:

npgettext(msgctxt, msgid, msgid_plural, n)
npgettext(msgctxt, msgids, n)  # msgids = [msgid, msgid_plural]
np_(msgctxt, msgid, msgid_plural, n)
np_(msgctxt, msgids, n)  # msgids = [msgid, msgid_plural]

The npgettext is similar to the nsgettext function.

e.g.) np_("Special", "An apple", "%{num} Apples", num) == ns_("Special|An apple", "%{num} Apples", num)
  • msgctxt: the message context.

  • 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.



358
359
360
361
362
363
364
# File 'lib/gettext.rb', line 358

def npgettext(msgctxt, arg1, arg2 = nil)
  if arg1.kind_of?(Array)
    nsgettext(msgctxt + "\004" + arg1[0], arg1[1], arg2, nil)
  else
    nsgettext(msgctxt + "\004" + arg1, arg2, arg3, nil)
  end
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.



146
147
148
149
150
151
152
153
# File 'lib/gettext.rb', line 146

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