gettext/textdomain_group - GetText::TextDomainGroup class
Copyright (C) 2009 Masao Mutoh
You may redistribute it and/or modify it under the same license terms as Ruby or LGPL.
version - version information of gettext
Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com> Copyright (C) 2005-2009 Masao Mutoh
You may redistribute it and/or modify it under the same license terms as Ruby or LGPL.
TODO: MsgMerge should use PoMessage to generate PO content.
Copyright (C) 2012 Kouhei Sutou <kou@clear-code.com> Copyright (C) 2010 masone (Christian Felder) <ema@rh-productions.ch> Copyright (C) 2009 Masao Mutoh
License: Ruby's or LGPL
This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <www.gnu.org/licenses/>.
This is the module for backward compatibility, but GetText::Tools::XGetText.run should be used.
makes dynamic translation messages readable for the gettext parser. _(fruit) cannot be understood by the gettext parser. To help the parser find all your translations, you can add fruit = N_("Apple") which does not translate, but tells the parser: "Apple" needs translation.
msgid: the message id.
Returns: msgid.
# File lib/gettext.rb, line 245 def N_(msgid) msgid end
This is same function as N_ but for ngettext.
msgid: the message id.
msgid_plural: the plural message id.
Returns: msgid.
# File lib/gettext.rb, line 253 def Nn_(msgid, msgid_plural) [msgid, msgid_plural] end
bindtextdomain(domainname, options = {})
Bind a textdomain(%{path}/%{locale}/LC_MESSAGES/%{domainname}.mo) to your program. Normally, the texdomain scope becomes the class/module(and parent classes/included modules).
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)
:output_charset - The output charset. Same with GetText.set_output_charset. Usually, L10n library doesn't use this option. Application may use this once.
Returns: the GetText::TextDomainManager.
# File lib/gettext.rb, line 64 def bindtextdomain(domainname, *options) bindtextdomain_to(self, domainname, *options) end
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.
# File lib/gettext.rb, line 72 def bindtextdomain_to(klass, domainname, *options) if options[0].kind_of? Hash opts = options[0] else # for backward compatibility. opts = {} opts[:path] = options[0] if options[0] opts[:output_charset] = options[2] if options[2] end unless (klass.kind_of? GetText or klass.include? GetText) klass.__send__(:include, GetText) end TextDomainManager.bind_to(klass, domainname, opts) end
Gets the CGI object. If it is nil, returns new CGI object. This methods is appeared when requiring "gettext/cgi".
Returns: the CGI object
# File lib/gettext/cgi.rb, line 35 def cgi Locale.cgi end
Same as GetText.set_cgi. This methods is appeared when requiring "gettext/cgi".
cgi_: CGI object
Returns: cgi_
# File lib/gettext/cgi.rb, line 28 def cgi=(cgi_) set_cgi(cgi_) cgi_ end
Creates mo-files using #{po_root}/#{lang}/*.po an put them to #{targetdir}/#{targetdir_rule}/.
This is a convenience function of GetText.rmsgfmt for multiple target files.
options: options as a Hash.
verbose: true if verbose mode, otherwise false
po_root: the root directory of po-files.
mo_root: the target root directory where the mo-files are stored.
mo_path_rule: the target directory for each mo-files.
# File lib/gettext/tools.rb, line 101 def create_mofiles(options = {}) options = {:po_root => "./po"}.merge(options) Dir.glob(File.join(options[:po_root], "*/*.po")) do |po_file| mo_file = mo_file_from_po_file(po_file,options) $stderr.print %[#{po_file} -> #{mo_file} ... ] if options[:verbose] FileUtils.mkdir_p(File.dirname(mo_file)) Tools::MsgFmt.run(po_file, "-o", mo_file) $stderr.puts "Done." if options[:verbose] end 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".
msgid: the message id.
Returns: localized text by msgid. If there are not binded mo-file, it will return msgid.
# File lib/gettext.rb, line 128 def gettext(msgid) TextDomainManager.translate_singular_message(self, msgid) 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 rxgettext); 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
# File lib/gettext/tools.rb, line 69 def msgmerge(defpo, refpo, app_version, options={}) verbose = options.delete(:verbose) puts "msgmerge called" if verbose $stderr.print defpo + " " content = merge_po_files(defpo,refpo,options.delete(:msgmerge),verbose) if content.empty? # report failure 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 # update version and save merged data content.sub!(/(Project-Id-Version\:).*$/, "\\1 #{app_version}\\n\"") File.open(defpo, "w") {|f|f.write(content)} end self 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.
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.
# File lib/gettext.rb, line 180 def ngettext(msgid, msgid_plural, n = nil) TextDomainManager.translate_plural_message(self, msgid, msgid_plural, n) end
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.
# File lib/gettext.rb, line 217 def npgettext(msgctxt, msgids, arg2 = nil, arg3 = nil) if msgids.kind_of?(Array) msgid = msgids[0] msgid_ctxt = "#{msgctxt}\0004#{msgid}" msgid_plural = msgids[1] opt1 = arg2 opt2 = arg3 else msgid = msgids msgid_ctxt = "#{msgctxt}\0004#{msgid}" msgid_plural = arg2 opt1 = arg3 opt2 = nil end msgstr = TextDomainManager.translate_plural_message(self, msgid_ctxt, msgid_plural, opt1, opt2) if msgstr == msgid_ctxt msgid else msgstr end end
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.
# File lib/gettext.rb, line 199 def nsgettext(msgid, msgid_plural, n="|", seperator = "|") TextDomainManager.translate_plural_message(self, msgid, msgid_plural, n, seperator) end
Gets the current output_charset which is set using GetText.set_output_charset.
Returns: output_charset.
# File lib/gettext.rb, line 268 def output_charset TextDomainManager.output_charset end
This is the workaround to conflict p_ methods with the xx("double x") library. rubyforge.org/projects/codeforpeople/
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
# File lib/gettext.rb, line 162 def pgettext(msgctxt, msgid) TextDomainManager.translate_singular_message(self, "#{msgctxt}\0004#{msgid}", "\0004") end
Sets a CGI object. This methods is appeared when requiring "gettext/cgi".
cgi_: CGI object
Returns: self
# File lib/gettext/cgi.rb, line 21 def set_cgi(cgi_) Locale.set_cgi(cgi_) end
Set the locale to the current thread. Note that if set_locale is set, this value is ignored. If you need, set_locale(nil); set_current_locale(lang)
# File lib/gettext.rb, line 284 def set_current_locale(lang) Locale.current = lang end
Set the locale. This value forces the locale whole the programs. This method calls Locale.set_app_language_tags, Locale.default, Locale.current. Use Locale methods if you need to handle locales more flexible.
# File lib/gettext.rb, line 275 def set_locale(lang) Locale.set_app_language_tags(lang) Locale.default = lang Locale.current = lang end
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: self
# File lib/gettext.rb, line 261 def set_output_charset(charset) TextDomainManager.output_charset = charset self end
Translates msgid, but if there are no localized text, it returns a last part of msgid separeted "div".
msgid: the message id.
separator: separator or nil for no seperation.
Returns: the localized text by msgid. If there are no localized text, it returns a last part of the msgid separeted by "seperator". Movie|Location -> Location
See: www.gnu.org/software/gettext/manual/html_mono/gettext.html#SEC151
# File lib/gettext.rb, line 145 def sgettext(msgid, seperator = "|") TextDomainManager.translate_singular_message(self, msgid, seperator) end
At first, this creates the #{po_root}/#{domainname}.pot file using GetText::XGetText.run. In the second step, 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, that should be parsed for messages (See GetText::XGetText.run for more details).
app_version: the application information which appears "Project-Id-Version: #{app_version}" in the pot/po-files.
options: a hash with following possible settings
:lang - update files only for one language - the language specified by this option :po_root - the root directory of po-files :msgmerge - an array with the options, passed through to the gnu msgmerge tool symbols are automatically translated to options with dashes, example: [:no_wrap, :no_fuzzy_matching, :sort_output] translated to '--no-fuzzy-matching --sort-output' :verbose - true to show verbose messages. default is false.
Example: GetText.update_pofiles("myapp", Dir.glob("lib/*.rb"), "myapp 1.0.0", :verbose => true)
# File lib/gettext/tools.rb, line 135 def update_pofiles(textdomain, files, app_version, options = {}) puts options.inspect if options[:verbose] #write found messages to tmp.pot temp_pot = "tmp.pot" Tools::XGetText.run("-o", temp_pot, *files) #merge tmp.pot and existing pot po_root = options.delete(:po_root) || "po" FileUtils.mkdir_p(po_root) msgmerge("#{po_root}/#{textdomain}.pot", temp_pot, app_version, options.dup) #update local po-files only_one_language = options.delete(:lang) if only_one_language msgmerge("#{po_root}/#{only_one_language}/#{textdomain}.po", temp_pot, app_version, options.dup) else Dir.glob("#{po_root}/*/#{textdomain}.po") do |po_file| msgmerge(po_file, temp_pot, app_version, options.dup) end end File.delete(temp_pot) end
Generated with the Darkfish Rdoc Generator 2.