X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fload-gettext.rb;h=6377b88232fb2d7d5eea7d4d25c94615f8a0c736;hb=263e939f1a8b8adf35afc33ab547dff8d4b97a4e;hp=1d7cb88b3b6f505175a8986dd7b4c34e75dc2ae1;hpb=d2d414a0fec91dfc2d79705b6e974f8830561dc5;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/load-gettext.rb b/lib/rbot/load-gettext.rb index 1d7cb88b..6377b882 100644 --- a/lib/rbot/load-gettext.rb +++ b/lib/rbot/load-gettext.rb @@ -1,48 +1,152 @@ -# load gettext module and provide fallback in case of failure +#-- vim:sw=2:et +#++ +# +# :title: GetText interface for rbot +# +# Load gettext module and provide fallback in case of failure class GetTextVersionError < Exception end # try to load gettext, or provide fake getttext functions begin +# workaround for gettext not checking empty LANGUAGE +if ENV["LANGUAGE"] and ENV["LANGUAGE"].empty? + ENV.delete "LANGUAGE" +end + require 'gettext/version' gettext_version = GetText::VERSION.split('.').map {|n| n.to_i} - include Comparable # required for Array#between? - unless gettext_version.between? [1, 8, 0], [1, 10, 0] - raise GetTextVersionError, "Unsupported ruby-gettext version installed: #{gettext_version.join '.'}; supported versions are 1.8.0-1.10.0" + class ::Array + include Comparable # for Array#>= + end + unless gettext_version >= [1, 8, 0] + raise GetTextVersionError, "Unsupported ruby-gettext version installed: #{gettext_version.join '.'}; supported versions are 1.8.0 and above" end require 'gettext' include GetText - bindtextdomain 'rbot' + rbot_locale_path = File.join(Irc::Bot::Config.datadir, + gettext_version < [2, 2, 0] ? + "../locale/%{locale}/LC_MESSAGES/%{name}.mo" : + "../locale/%{lang}/LC_MESSAGES/%{name}.mo") + + if gettext_version < [2, 0, 0] + add_default_locale_path(rbot_locale_path) + else + LocalePath.add_default_rule(rbot_locale_path) + end + + if GetText.respond_to? :cached= + GetText.cached = false + elsif TextDomain.respond_to? :cached= + TextDomain.cached = false + else + warning 'This version of ruby-gettext does not support non-cached mode; mo files are not reloaded when setting language' + end + + begin + bindtextdomain 'rbot' + rescue NoMethodError => e + error e + warning 'Trying to work around RubyGems/GetText incompatibility' + module ::Gem + def self.all_load_paths + result = [] + + Gem.path.each do |gemdir| + each_load_path all_partials(gemdir) do |load_path| + result << load_path + end + end + + result + end + end + retry + end + + module GetText - # patch for ruby-gettext 1.8.0 up to 1.10.0 (and more?) to cope with anonymous - # modules used by rbot - # FIXME remove the patch when ruby-gettext is fixed, or rbot switches to named modules - if !instance_methods.include?('orig_bound_targets') + # patch for ruby-gettext 1.x to cope with anonymous modules used by rbot. + # bound_targets and related methods are not used nor present in 2.x, and + # this patch is not needed + if respond_to? :bound_targets, true alias :orig_bound_targets :bound_targets + + def bound_targets(*a) # :nodoc: + bt = orig_bound_targets(*a) rescue [] + bt.empty? ? orig_bound_targets(Object) : bt + end end - def bound_targets(*a) # :nodoc: - orig_bound_targets(*a) rescue orig_bound_targets(Object) - end - end - begin require 'stringio' - gettext_info = StringIO.new - current_textdomain_info(:out=>gettext_info) # fails sometimes - debug 'using ruby-gettext' - gettext_info.string.each_line {|l| debug l} - rescue Exception - warn "ruby-gettext was loaded but appears to be non-functional. maybe an mo file doesn't exist for your locale." + + # GetText 2.1.0 does not provide current_textdomain_info, + # so we adapt the one from 1.9.10 + # TODO we would _really_ like to have a future-proof version of this, + # but judging by the ruby gettext source code, this isn't going to + # happen anytime soon. + if not respond_to? :current_textdomain_info + # 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. + def current_textdomain_info(options = {}) + opts = {:with_messages => false, :with_paths => false, :out => STDOUT}.merge(options) + ret = nil + # this is for 2.1.0 + TextDomainManager.each_text_domains(self) {|textdomain, lang| + opts[:out].puts "TextDomain name: #{textdomain.name.inspect}" + opts[:out].puts "TextDomain current locale: #{lang.to_s.inspect}" + opts[:out].puts "TextDomain current mo path: #{textdomain.instance_variable_get(:@locale_path).current_path(lang).inspect}" + 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 + end + + # This method is used to output debug information on the GetText + # textdomain, and it's called by the language setting routines + # in rbot + def rbot_gettext_debug + begin + gettext_info = StringIO.new + current_textdomain_info(:out => gettext_info) # fails sometimes + rescue Exception + warning "failed to retrieve textdomain info. maybe an mo file doesn't exist for your locale." + debug $! + ensure + gettext_info.string.each_line { |l| debug l} + end + end end + log "gettext loaded" + rescue LoadError, GetTextVersionError - warn "failed to load ruby-gettext package: #{$!}; translations are disabled" + warning "failed to load ruby-gettext package: #{$!}; translations are disabled" + + # undefine GetText, in case it got defined because the error was caused by a + # wrong version + if defined?(GetText) + Object.module_eval { remove_const("GetText") } + end # dummy functions that return msg_id without translation def _(s) @@ -65,6 +169,9 @@ rescue LoadError, GetTextVersionError args[0] end + def bindtextdomain_to(*args) + end + # the following extension to String#% is from ruby-gettext's string.rb file. # it needs to be included in the fallback since the source already use this form