lib/gettext.rb in gettext-1.91.0 vs lib/gettext.rb in gettext-1.92.0

- old
+ new

@@ -8,11 +8,11 @@ Masahiro Sakai <s01397ms@sfc.keio.ac.jp> You may redistribute it and/or modify it under the same license terms as Ruby. - $Id: gettext.rb,v 1.40 2008/05/05 17:38:38 mutoh Exp $ + $Id: gettext.rb,v 1.43 2008/07/26 18:16:28 mutoh Exp $ =end require 'rbconfig' require 'gettext/version' require 'gettext/mo' @@ -171,17 +171,18 @@ end @@__cache_target_classes = {} 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 - 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 @@ -199,17 +200,17 @@ end @@__cache_bound_target = {} def bound_target(klass = self) # :nodoc: - id = klass.object_id + ret = (klass.kind_of? Module) ? klass : klass.class + id = ret.object_id if cached? tgt = @@__cache_bound_target[id] return tgt if tgt end - ret = (klass.kind_of? Module) ? klass : klass.class 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 @@ -301,10 +302,27 @@ end end @@__cache_msgids[cached_key] = msg end + # 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: http://www.gnu.org/software/autoconf/manual/gettext/Contexts.html + def pgettext(msgctxt, msgid) + sgettext(msgctxt + "\004" + msgid, "\004") + end + @@__cache_nmsgids = {} # call-seq: # ngettext(msgid, msgid_plural, n) # ngettext(msgids, n) # msgids = [msgid, msgid_plural] @@ -321,10 +339,32 @@ # "plural-rule" is defined in po-file. def ngettext(arg1, arg2, arg3 = nil) nsgettext(arg1, arg2, arg3, nil) end + # 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. + 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 + # This function does nothing. But it is required in order to recognize the msgid by rgettext. # * msgid: the message id. # * Returns: msgid. def N_(msgid) msgid @@ -547,12 +587,14 @@ alias :setlocale :locale= #:nodoc: alias :_ :gettext #:nodoc: alias :n_ :ngettext #:nodoc: alias :s_ :sgettext #:nodoc: + alias :p_ :pgettext #:nodoc: alias :ns_ :nsgettext #:nodoc: module_function :bindtextdomain, :textdomain, :each_textdomain, :cached=, :cached?, :clear_cache, - :N_, :gettext, :_, :ngettext, :n_, :sgettext, :s_, :nsgettext, :ns_, :bound_target, :bound_targets, :find_targets, + :N_, :gettext, :_, :ngettext, :n_, :sgettext, :s_, :nsgettext, :ns_, :pgettext, :p_, + :bound_target, :bound_targets, :find_targets, :setlocale, :set_locale, :locale=, :set_locale_all, :locale, :set_output_charset, :output_charset=, :output_charset, :current_textdomain_info, :remove_all_textdomains end