X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fload-gettext.rb;h=252e1e344f1830d8aa5e41f461778ed6215ce61d;hb=3342d4b9a510655d320590f7333f616355d0201d;hp=505a28bc4aec7442c80ed8eae1f177b7d1d3b3c4;hpb=beefd7fc3973320ee6ecc6ac0b6201343ed2549d;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/load-gettext.rb b/lib/rbot/load-gettext.rb index 505a28bc..252e1e34 100644 --- a/lib/rbot/load-gettext.rb +++ b/lib/rbot/load-gettext.rb @@ -10,10 +10,17 @@ 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 # for Array#>= + 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 @@ -22,7 +29,11 @@ begin include GetText - rbot_locale_path = File.join(Irc::Bot::Config.datadir, "../locale/%{locale}/LC_MESSAGES/%{name}.mo") + 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 @@ -36,13 +47,35 @@ begin else warning 'This version of ruby-gettext does not support non-cached mode; mo files are not reloaded when setting language' end - bindtextdomain 'rbot' + + 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.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 instance_methods.include?('bound_targets') and not instance_methods.include?('orig_bound_targets') + if respond_to? :bound_targets, true alias :orig_bound_targets :bound_targets def bound_targets(*a) # :nodoc: @@ -53,6 +86,41 @@ begin require 'stringio' + # 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_textdomains(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 @@ -61,7 +129,8 @@ begin gettext_info = StringIO.new current_textdomain_info(:out => gettext_info) # fails sometimes rescue Exception - warning "gettext failed to set call textdomain info. maybe an mo file doesn't exist for your locale." + 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