VERSION | = | "1.91.0" |
Add default locale path.
# File lib/gettext.rb, line 511 511: def add_default_locale_path(path) 512: TextDomain.add_default_locale_path(path) 513: end
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.
Note: Don‘t use locale_, charset argument(not in options). They are remained for backward compatibility.
# File lib/gettext.rb, line 85 85: def bindtextdomain(domainname, options = {}, locale_ = nil, charset = nil) 86: opt = {} 87: if options.kind_of? String 88: # For backward compatibility 89: opt = {:path => options, :locale => locale_, :charset => charset} 90: elsif options 91: opt = options 92: end 93: opt[:locale] = opt[:locale] ? Locale::Object.new(opt[:locale]) : Locale.get 94: opt[:charset] = TextDomainManager.output_charset if TextDomainManager.output_charset 95: opt[:locale].charset = opt[:charset] if opt[:charset] 96: Locale.set_current(opt[:locale]) 97: target_key = bound_target 98: manager = @@__textdomainmanagers[target_key] 99: if manager 100: manager.set_locale(opt[:locale]) 101: else 102: manager = TextDomainManager.new(target_key, opt[:locale]) 103: @@__textdomainmanagers[target_key] = manager 104: end 105: manager.add_textdomain(domainname, opt) 106: manager 107: end
Includes GetText module and bind a textdomain to a class.
# File lib/gettext.rb, line 113 113: def bindtextdomain_to(klass, domainname, options = {}) 114: ret = nil 115: klass.module_eval { 116: include GetText 117: ret = bindtextdomain(domainname, options) 118: } 119: ret 120: end
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.
# File lib/gettext.rb, line 42 42: def cached=(val) 43: @@__cached = val 44: GetText::TextDomain.check_mo = ! val 45: end
Gets the CGI object. If it is nil, returns new CGI object.
# File lib/gettext/cgi.rb, line 36 36: def cgi 37: Locale.cgi 38: end
Same as GetText.set_cgi.
# File lib/gettext/cgi.rb, line 29 29: def cgi=(cgi_) 30: set_cgi(cgi_) 31: cgi_ 32: end
Clear the cached messages.
# File lib/gettext.rb, line 53 53: def clear_cache 54: @@__cache_msgids = {} 55: @@__cache_nmsgids = {} 56: @@__cache_target_classes = {} 57: @@__cache_bound_target = {} 58: @@__cache_bound_targets = {} 59: end
Creates mo-files using #{po_root}/#{lang}/*.po an put them to #{targetdir}/#{targetpath_rule}/.
This is a convenience function of GetText.rmsgfmt for plural target files.
# File lib/gettext/utils.rb, line 96 96: def create_mofiles(verbose = false, 97: podir = "./po", targetdir = "./data/locale", 98: targetpath_rule = "%s/LC_MESSAGES") 99: 100: modir = File.join(targetdir, targetpath_rule) 101: Dir.glob(File.join(podir, "*/*.po")) do |file| 102: lang, basename = /\/([^\/]+?)\/(.*)\.po/.match(file[podir.size..-1]).to_a[1,2] 103: outdir = modir % lang 104: FileUtils.mkdir_p(outdir) unless File.directory?(outdir) 105: rmsgfmt(file, File.join(outdir, "#{basename}.mo")) 106: if verbose 107: $stderr.puts %Q[#{file} -> #{File.join(outdir, "#{basename}.mo")}] 108: end 109: end 110: end
Show the current textdomain information. This function is for debugging.
# File lib/gettext.rb, line 520 520: def current_textdomain_info(options = {}) 521: opts = {:with_messages => false, :with_paths => false, :out => STDOUT}.merge(options) 522: ret = nil 523: each_textdomain {|textdomain| 524: opts[:out].puts "TextDomain name: \"#{textdomain.name}\"" 525: opts[:out].puts "TextDomain current locale: \"#{textdomain.current_locale}\"" 526: opts[:out].puts "TextDomain current mo filename: \"#{textdomain.current_mo.filename}\"" 527: if opts[:with_paths] 528: opts[:out].puts "TextDomain locale file paths:" 529: textdomain.locale_paths.each do |v| 530: opts[:out].puts " #{v}" 531: end 532: end 533: if opts[:with_messages] 534: opts[:out].puts "The messages in the mo file:" 535: textdomain.current_mo.each{|k, v| 536: opts[:out].puts " \"#{k}\": \"#{v}\"" 537: } 538: end 539: } 540: end
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}") % {:val => "world"}
Because the translator may want to change the position of "world".
# File lib/gettext.rb, line 262 262: def gettext(msgid) 263: sgettext(msgid, nil) 264: end
Gets the current locale.
# File lib/gettext.rb, line 503 503: def locale 504: Locale.current 505: end
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.
# File lib/gettext.rb, line 473 473: def locale=(locale) 474: Locale.default = locale 475: set_locale_all(locale) 476: Locale.default 477: end
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.
# File lib/gettext/utils.rb, line 47 47: def msgmerge(defpo, refpo, app_version) 48: $stderr.puts defpo 49: cmd = ENV["MSGMERGE_PATH"] || "msgmerge" 50: 51: cont = "" 52: if FileTest.exist? defpo 53: `#{cmd} --version` 54: unless $?.success? 55: raise _("`#{cmd}' may not be found. \nInstall GNU Gettext then set PATH or MSGMERGE_PATH correctly.") 56: end 57: cont = `#{cmd} #{defpo} #{refpo}` 58: else 59: File.open(refpo) do |io| 60: cont = io.read 61: end 62: end 63: if cont.empty? 64: failed_filename = refpo + "~" 65: FileUtils.cp(refpo, failed_filename) 66: $stderr.puts _("Failed to merge with %{defpo}") % {:defpo => defpo} 67: $stderr.puts _("New .pot was copied to %{failed_filename}") %{:failed_filename => failed_filename} 68: raise _("Check these po/pot-files. It may have syntax errors or something wrong.") 69: else 70: cont.sub!(/(Project-Id-Version\:).*$/, "\\1 #{app_version}\\n\"") 71: File.open(defpo, "w") do |out| 72: out.write(cont) 73: end 74: end 75: self 76: end
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.
# File lib/gettext.rb, line 322 322: def ngettext(arg1, arg2, arg3 = nil) 323: nsgettext(arg1, arg2, arg3, nil) 324: end
The nsgettext is similar to the ngettext. But if there are no localized text, it returns a last part of msgid separeted "div".
# File lib/gettext.rb, line 357 357: def nsgettext(arg1, arg2, arg3 = "|", arg4 = "|") 358: if arg1.kind_of?(Array) 359: msgid = arg1[0] 360: msgid_plural = arg1[1] 361: n = arg2 362: if arg3 and arg3.kind_of? Numeric 363: raise ArgumentError, _("3rd parmeter is wrong: value = %{number}") % {:number => arg3} 364: end 365: div = arg3 366: else 367: msgid = arg1 368: msgid_plural = arg2 369: n = arg3 370: div = arg4 371: end 372: 373: cached_key = [bound_target, Locale.current, msgid + "\000" + msgid_plural] 374: msgs = nil 375: if @@__cached 376: if @@__cache_nmsgids.has_key?(cached_key) 377: msgs = @@__cache_nmsgids[cached_key] # [msgstr, cond_as_string] 378: end 379: end 380: unless msgs 381: # Use "for"(not "each") to support JRuby 1.1.0. 382: for target in bound_targets(self) 383: manager = @@__textdomainmanagers[target] 384: for textdomain in manager.textdomains 385: msgs = textdomain[1].ngettext_data(msgid, msgid_plural) 386: break if msgs 387: end 388: break if msgs 389: end 390: msgs = [[msgid, msgid_plural], "n != 1"] unless msgs 391: @@__cache_nmsgids[cached_key] = msgs 392: end 393: msgstrs = msgs[0] 394: if div and msgstrs[0] == msgid 395: if index = msgstrs[0].rindex(div) 396: msgstrs[0] = msgstrs[0][(index + 1)..-1] 397: end 398: end 399: plural = eval(msgs[1]) 400: if plural.kind_of?(Numeric) 401: ret = msgstrs[plural] 402: else 403: ret = plural ? msgstrs[1] : msgstrs[0] 404: end 405: ret 406: end
Gets the current output_charset which is set using GetText.set_output_charset.
# File lib/gettext.rb, line 497 497: def output_charset 498: TextDomainManager.output_charset || locale.charset 499: end
Same as GetText.set_output_charset
# File lib/gettext.rb, line 491 491: def output_charset=(charset) 492: TextDomainManager.output_charset = charset 493: end
for testing.
# File lib/gettext.rb, line 543 543: def remove_all_textdomains 544: clear_cache 545: @@__textdomainmanagers = {} 546: end
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.
# File lib/gettext/rgettext.rb, line 259 259: def rgettext(targetfiles = nil, out = STDOUT) 260: RGetText.run(targetfiles, out) 261: self 262: end
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).
# File lib/gettext/rmsgfmt.rb, line 79 79: def rmsgfmt(targetfile = nil, output_path = nil) 80: RMsgfmt.run(targetfile, output_path) 81: end
Experimental
# File lib/gettext/rmsgmerge.rb, line 489 489: def rmsgmerge(reference = nil, definition = nil, out = STDOUT) 490: RMsgMerge.run(reference, definition, out) 491: end
Sets a CGI object.
# File lib/gettext/cgi.rb, line 22 22: def set_cgi(cgi_) 23: Locale.set_cgi(cgi_) 24: end
Sets the current locale to the current class/module
Notice that you shouldn‘t use this for your own Libraries.
Otherwise, this changes the locale of the current class/module and its ancestors. Default is false.
# File lib/gettext.rb, line 416 416: def set_locale(locale, this_target_only = false) 417: ret = nil 418: if locale 419: if locale.kind_of? Locale::Object 420: ret = locale 421: else 422: ret = Locale::Object.new(locale.to_s) 423: end 424: ret.charset = TextDomainManager.output_charset if TextDomainManager.output_charset 425: Locale.set(ret) 426: else 427: Locale.set(nil) 428: ret = Locale.get 429: end 430: if this_target_only 431: manager = @@__textdomainmanagers[bound_target] 432: if manager 433: manager.set_locale(ret, ! cached?) 434: end 435: else 436: each_textdomain {|textdomain| 437: textdomain.set_locale(ret, ! cached?) 438: } 439: end 440: self 441: end
Sets current locale to the all textdomains.
Note that you shouldn‘t use this for your own Libraries.
# File lib/gettext.rb, line 448 448: def set_locale_all(locale) 449: ret = nil 450: if locale 451: if locale.kind_of? Locale::Object 452: ret = locale 453: else 454: ret = Locale::Object.new(locale.to_s) 455: end 456: else 457: ret = Locale.default 458: end 459: ret.charset = TextDomainManager.output_charset if TextDomainManager.output_charset 460: Locale.set_current(ret) 461: TextDomainManager.each_all {|textdomain| 462: textdomain.set_locale(ret, ! cached?) 463: } 464: self 465: end
Sets charset(String) such as "euc-jp", "sjis", "CP932", "utf-8", … You shouldn‘t use this in your own Libraries.
# File lib/gettext.rb, line 483 483: def set_output_charset(charset) 484: TextDomainManager.output_charset = charset 485: self 486: end
Translates msgid, but 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
# File lib/gettext.rb, line 278 278: def sgettext(msgid, div = '|') 279: cached_key = [bound_target, Locale.current, msgid] 280: if cached? 281: if @@__cache_msgids[cached_key] 282: return @@__cache_msgids[cached_key] 283: end 284: end 285: msg = nil 286: 287: # Use "for"(not "each") to support JRuby 1.1.0. 288: for target in bound_targets(self) 289: manager = @@__textdomainmanagers[target] 290: for textdomain in manager.textdomains 291: msg = textdomain[1].gettext(msgid) 292: break if msg 293: end 294: break if msg 295: end 296: 297: msg ||= msgid 298: if div and msg == msgid 299: if index = msg.rindex(div) 300: msg = msg[(index + 1)..-1] 301: end 302: end 303: @@__cache_msgids[cached_key] = msg 304: end
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.
# File lib/gettext.rb, line 128 128: def textdomain(domainname) 129: domain = TextDomainManager.textdomain(domainname) 130: raise NoboundTextDomainError, "#{domainname} is not bound." unless domain 131: target_key = bound_target 132: manager = @@__textdomainmanagers[target_key] 133: unless manager 134: manager = TextDomainManager.new(target_key, Locale.get) 135: @@__textdomainmanagers[target_key] = manager 136: end 137: manager.set_locale(Locale.get) 138: manager.add_textdomain(domainname) 139: manager 140: end
Includes GetText module and bind an exsited textdomain to a class. See textdomain for more detail.
# File lib/gettext.rb, line 146 146: def textdomain_to(klass, domainname) 147: ret = nil 148: klass.module_eval { 149: include GetText 150: ret = textdomain(domainname) 151: } 152: ret 153: end
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.
(e.g.) GetText.update_pofiles("myapp", Dir.glob("lib/*.rb"), "myapp 1.0.0")
# File lib/gettext/utils.rb, line 127 127: def update_pofiles(textdomain, files, app_version, po_root = "po", refpot = "tmp.pot") 128: rgettext(files, refpot) 129: msgmerge_all(textdomain, app_version, po_root, refpot) 130: File.delete(refpot) 131: end