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.10.0"
@@__cached =
true
@@__textdomainmanagers =
Hash.new
@@__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:



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

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.



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

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] = output_charset if output_charset
  locale.charset = opt[:charset] if opt[:charset]
  Locale.set_current(opt[:locale])
  manager = @@__textdomainmanagers[bound_target]
  if manager
    manager.set_locale(opt[:locale]) 
  else
    manager = TextDomainManager.new(bound_target, opt[:locale])
    @@__textdomainmanagers[bound_target] = manager
  end
  manager.add_textdomain(domainname, opt)
  manager
end

.bound_targetObject

:nodoc:



165
166
167
168
169
170
171
# File 'lib/gettext.rb', line 165

def bound_target # :nodoc:
  if self.kind_of? Class or self.kind_of? Module
    self
  else
    self.class
  end
end

.bound_targets(klass) ⇒ Object

:nodoc:



173
174
175
176
177
178
179
180
# File 'lib/gettext.rb', line 173

def bound_targets(klass)  # :nodoc:
  ret = []
  ary = klass.name.split(/::/)
  while(v = ary.shift)
    ret.unshift(((ret.size == 0) ? eval(v) : ret[0].const_get(v)))
  end
  ((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



61
62
63
# File 'lib/gettext/cgi.rb', line 61

def cgi
  Locale.cgi
end

.cgi=(cgi_) ⇒ Object

Same as GetText.set_cgi.

  • cgi_: CGI object

  • Returns: cgi_



54
55
56
57
# File 'lib/gettext/cgi.rb', line 54

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

.charset=(cs) ⇒ Object

:nodoc:



430
431
432
433
# File 'lib/gettext.rb', line 430

def charset=(cs) #:nodoc:
  set_charset(cs)
  cs
end

.clear_cacheObject

Clear the cached messages.



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

def clear_cache
  @@__cache_msgids = {}
  @@__cache_nmsgids = {}
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.



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

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.

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



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/gettext.rb', line 449

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 = bound_target, 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.



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gettext.rb', line 151

def each_textdomain(klass = bound_target, 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

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



191
192
193
# File 'lib/gettext.rb', line 191

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



420
421
422
# File 'lib/gettext.rb', line 420

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



390
391
392
393
394
# File 'lib/gettext.rb', line 390

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 “msgmerge” tool is 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
# 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} - skipping!") % {:defpo => defpo}
    $stderr.puts _("Please check new .pot in %{failed_filename}") %{:failed_filename => failed_filename}
  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:



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

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.



251
252
253
# File 'lib/gettext.rb', line 251

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:



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

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.



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

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:



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/gettext.rb', line 475

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 or ! $DEBUG)
    if @@__cache_nmsgids.keys.include?(cached_key)
      msgs = @@__cache_nmsgids[cached_key]  # [msgstr, cond_as_string]
    end
  end
  unless msgs
    each_textdomain {|textdomain|
      msgs = textdomain.ngettext_data(msgid, msgid_plural)
      break if msgs
    }
    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.



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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/gettext.rb', line 279

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 or ! $DEBUG)
    if @@__cache_nmsgids.keys.include?(cached_key)
      msgs = @@__cache_nmsgids[cached_key]  # [msgstr, cond_as_string]
    end
  end
  unless msgs
    each_textdomain {|textdomain|
      msgs = textdomain.ngettext_data(msgid, msgid_plural)
      break if msgs
    }
    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.



414
415
416
# File 'lib/gettext.rb', line 414

def output_charset
  TextDomainManager.output_charset
end

.output_charset=(charset) ⇒ Object

Same as GetText.set_output_charset

  • charset: an output_charset

  • Returns: charset



408
409
410
# File 'lib/gettext.rb', line 408

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:



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/gettext.rb', line 474

def sgettext(msgid, div = '|')
  cached_key = [bound_target, Locale.current, msgid]
  if (@@__cached or ! $DEBUG)
    if @@__cache_msgids.keys.include?(cached_key)
      return @@__cache_msgids[cached_key]
    end
  end
  msg = nil
  each_textdomain {|textdomain|
    msg = textdomain.gettext(msgid)
    break if msg
  }
  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



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

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

.set_charset(cs) ⇒ Object

Deprecated. Now this function do nothing. Use GetText.output_charset= instead.



425
426
427
428
# File 'lib/gettext.rb', line 425

def set_charset(cs) #:nodoc:
  $stderr.puts "Deprecated. Now this function do nothing. Use GetText.output_charset= instead." if $DEBUG
  self
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



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/gettext.rb', line 333

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 = output_charset if 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)
    end
  else
    each_textdomain {|textdomain|
	textdomain.set_locale(ret)
    }
  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



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/gettext.rb', line 365

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 = output_charset if output_charset
  Locale.set_current(ret)
  TextDomainManager.each_all {|textdomain|
    textdomain.set_locale(ret)
  }
  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



400
401
402
403
# File 'lib/gettext.rb', line 400

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:



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

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/gettext.rb', line 207

def sgettext(msgid, div = '|')
  cached_key = [bound_target, Locale.current, msgid]
  if (@@__cached or ! $DEBUG)
    if @@__cache_msgids.keys.include?(cached_key)
      return @@__cache_msgids[cached_key]
    end
  end
  msg = nil
  each_textdomain {|textdomain|
    msg = textdomain.gettext(msgid)
    break if msg
  }
  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.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gettext.rb', line 121

def textdomain(domainname)
  domain = TextDomainManager.textdomain(domainname)
  raise NoboundTextDomainError, "#{domainname} is not bound." unless domain
  manager = @@__textdomainmanagers[bound_target]
  unless manager
    manager = TextDomainManager.new(bound_target, Locale.get)
    @@__textdomainmanagers[bound_target] = 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")


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

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



439
440
441
# File 'lib/gettext.rb', line 439

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.



106
107
108
109
110
111
112
113
# File 'lib/gettext.rb', line 106

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.



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

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.



138
139
140
141
142
143
144
145
# File 'lib/gettext.rb', line 138

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